How to save navigation properties in Entity infrastructure

I am trying to use a repository template to save an object using Entity Framework. I do not know how to save Navigation Properties (for example, the Account below). Can someone shed some light on this. Especially how it would be possible to set AccountId from the MVC controller all the way to the storage where it was stored.

Thanks!

--- Sample code ---

public void SavePerson(Person person)
    {           
        if (person != null)
        {
            using (xxxxxxEntities bbEntities = new xxxxxxEntities())
            {
                //see if it in the db
                Person cPerson;

                ObjectQuery<Person> persons = bbEntities.Person;

                cPerson = (from p in persons
                         where p.PersonId == person.PersonId
                         select p).FirstOrDefault() ?? new Person();

                //synch it
                cPerson.Account.AccountId = person.Account.AccountId; //<=== ????
                cPerson.Active = person.Active;
                cPerson.BirthDay = person.BirthDay;
                cPerson.BirthMonth = person.BirthMonth;
                cPerson.BirthYear = person.BirthYear;
                cPerson.CellPhone = person.CellPhone;
                cPerson.CreatedBy = person.CreatedBy;
                cPerson.CScore = person.CScore;

Etc....
+3
source share
1 answer

, . , MVC - , . , , , .

, ;

cPerson.Account = (from a in Account
                   where a.AccountId.Equals(person.Account.AccountId)
                   select a).FirstOrDefault();

Account , . , , , , Entity.

+3

Source: https://habr.com/ru/post/1702404/


All Articles