ef code first - EF CF - Setting foreign key does not set the entity -


i have client , assignmnet class. relationship client can have several assignments.

public class assignment {     public datetime created { get; set; }     public datetime? updated { get; set; }      [foreignkey("clientid")]     public virtual client client { get; set; }     [foreignkey("client")]     public int clientid { get; set; }              [key]     public int id { get; set; }    }  public class client {     public datetime created { get; set; }     public datetime? updated { get; set; }      public virtual ienumerable<assignment> assignments { get; set; }      [key]     public int id { get; set; } } 

the following code works. both client , clientid set right value.

var clientcontroller = getclientcontroller(); client c = new client(); clientcontroller.postclient(c);  var asscontroller = getassignmentcontroller(); assignment = new assignment(); a.client = c; asscontroller.postassignment(a); 

but following code not work. clientid updated client null.

var clientcontroller = getclientcontroller(); client c = new client(); clientcontroller.postclient(c);  var asscontroller = getassignmentcontroller(); assignment = new assignment(); a.clientid = c.id; asscontroller.postassignment(a); 

the assignment controller works following:

public httpresponsemessage postassignment(assignment assignment) {     if (!modelstate.isvalid)     {         return request.createerrorresponse(httpstatuscode.badrequest, modelstate);     }      try     {         _db.insert(assignment);         _db.save();         httpresponsemessage response = request.createresponse(httpstatuscode.created, assignment);         return response;     }     catch (exception ex)     {         return request.createerrorresponse(httpstatuscode.notfound, ex);     } } 

does know doing wrong?


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -