c# - Adding a new row to a table with many foregin keys -


i have small problem setting relations between tables in entity. have 3 models: appuser, assignment , completedassignment.

public class appuser {     [display(name = "your id:")]     public int id { get; set; }     [display(name = "first name")]     public string firstname { get; set; }     [display(name = "last name")]     public string lastname { get; set; }      public virtual icollection<completedassignment> completedassignments { get; set; } }  public class assignment {        [display(name="assignment id")]     public int id { get; set; }     [display(name = "assignment name")]     public string assignmentname { get; set; }     [display(name = "assignment description")]     [datatype(datatype.multilinetext)]     public string description { get; set; }      public datetime creationdate { get; set; }      public assignment(){         this.creationdate = datetime.now;     }      public virtual icollection<completedassignment> assignments { get; set; } }  public class completedassignment {     public int id { get; set; }     [display(name = "assignment id")]     public int assignmentid { get; set; }     [display(name = "student id")]     public int appuserid { get; set; }     public datetime completiondate{get;set;}      public completedassignment()     {         this.completiondate = datetime.now;     }      public virtual assignment assignment { get; set; }     public virtual appuser appuser { get; set; } } 

i (think) have configured set of relations between them: 1 many relation between appuser , completedassignment, , 1 many between assignment , completedassignment have 2 foreign keys in completedassignment table.

the problem occurs when i'm trying add new completedassignment database 2 foreign key id's supplied user.

the error message:

"the insert statement conflicted foreign key constraint \"fk_dbo.completedassignments_dbo.appusers_appuserid\". conflict occurred in database \"aspnet-name-20160527092700\", table \"dbo.appusers\", column 'id'.\r\nthe statement has been terminated."

and code causes it:

[httppost] public actionresult create([bind(include = "id,assignmentid,userid,completiondate")] completedassignment completedassignment) {     if (modelstate.isvalid)     {         db.completedassignments.add(completedassignment);          db.savechanges();          return redirecttoaction("index");     }      return view(completedassignment); } 

as far remember sql error means there has been non-existent key supplied query, have checked few times , keys supplied exist in both tables. i'm kind of struggling grasp how entity database interaction works, kind of appreciated:)

appuserid has no value.

you wrote

[bind(include = "id,assignmentid,userid,completiondate")] 

it should be

[bind(include = "id,assignmentid,appuserid,completiondate")] 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -