asp.net mvc - How To Save Navigation Properties in the Entity Framework -


I am trying to use repository patterns to save an entity using unit framework. How am I not clear about saving the navigation properties (like the account below) can anyone put light on it? Specifically, how can one be saved from the MVC controller?

  Public Zero Save Person (person) {If (person! = Null) {(xxxxxxEntities bbEntities = new xxxxxxEntities ()) // See that this DB person is in cPerson; ObjectQuery & LT; Person & gt; Person = bbnt CPerson = (in P of the individuals, where P.PSIIID == person. Choose PersonID P). FirstOver Default (?) ?? New person (); // synchronize it cPerson.Account.AccountId = person.Account.AccountId; // & lt; === ???? CPerson.Active = person.Active; CPerson.BirthDay = person.BirthDay; CPerson.BirthMonth = person.BirthMonth; CPerson.BirthYear = person.BirthYear; CPerson.CellPhone = person.CellPhone; CPerson.Created By = person.Created By; CPerson.CScore = person.CScore; Etc ....   

I think you are going through difficult ways in this regard. There are a lot of posts on the repository pattern, the way the best works with MVC is to get the item, then update it with the form, then save it. The item you are doing is passing through your repository, retrieving it and then updating it with an object.

But this is not the problem you have;

  CPerson.Account = (from the account where a.AccountId.Equals (person.Account.AccountId) select one). FirstOrDefault ();  

You need to set the account object as an example of the account that you are trying to reference in this way. You can, possibly, remove this code in a separate repository for the account, just make sure that they share the same unit reference.


Comments