Entity Framework Code First lazy loading after saving

I have a lookup table and data table in my db. For example, I will use gender and human. Therefore, we say that the gender table looks like this:

Id         Code
1          Male
2          Female

and the face table looks like this:

Id         Name             GenderId
1          Bob              1
2          Jane             2

I first modeled both tables in EF code as follows:

public class Gender
{
    public int Id {get;set;}
    public string Code {get;set;}
}

public class Person
{
    public int Id {get;set;}
    public string Name {get;set;}
    public int GenderId {get;set;}

    public virtual Gender {get;set;}
}

If I read a person already in the database, I can access man.Gender.Code without any problems. If I do this:

var person = new Person
             {
                 Name = "Bob",
                 GenderId = 1,
             };

context.People.Add(person);
context.SaveChanges();

var code = person.Gender.Code;

Then it will be correctly saved, but the last line will be denied, because the floor is zero. If I then open a new context and load the saved object, then the last line will work fine. Is there a way by which I can access the floor immediately after saving, as if I just loaded the object from the database?

+5
1

, new Person() POCO, , Gender. proxies.

DbSet.Create():

var person = context.People.Create();
person.Name = "Bob";
person.GenderId = 1;

context.People.Add(person);
context.SaveChanges();
+8

All Articles