I've a class:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int UserId { get; set; }
public virtual User User { get; set; }
}
In the User
Table:
Id Name
1 Tommy
2 John
I create an object:
Person P = new Person { Name = "Test", UserId = 1 };
I insert the Person
object into the database:
db.Persons.Add(P);
db.SaveChanges();
var user = P.User;
The problem is if I try to access P.User
it contains null
. Other properties of P
are populated properly.
Why?