c# - {"The INSERT statement conflicted with the FOREIGN KEY constraint The statement has been terminated."} -
there userstats table, want 1 one relationship, i'm getting above error when try , register user.thanks help!
user table:
public class user { [key] public int id { get; set; } public int userstatsid { get; set; } public virtual userstats userstats { get; set; } }
userstats table
[key] public int id { get; set; } public int userid { get; set;} [required] public user user { get; set; }
registerdetailscontroller
[httppost] [validateantiforgerytoken] public actionresult create(user user) { if (modelstate.isvalid) { db.users.add(user); db.savechanges(); if (user.gender == gender.male ) { return redirecttoaction("registermale", "registerstats", new {id = user.id }); } else { return redirecttoaction("registerfemale", "registerstats", new { id = user.id }); } } return view(user); }
you can't have true 1:1 relationship enforced entirely through foreign keys, inserts have done 1 @ time , doing violate constraint.
instead, have make relationship 1:0. making userstatsid
int?
instead of int
.
Comments
Post a Comment