Entity Framework: I set the foreign key, SaveChanges then access the navigation property, but it doesn't load the related entity. Why not?

前端 未结 4 1344
再見小時候
再見小時候 2020-12-02 10:50

I am using this Entity class with Entity Framework 5 Code First:

public class Survey
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public i         


        
4条回答
  •  再見小時候
    2020-12-02 11:22

    I expected the call to load the Client from the database because the foreign key exists. Why didn't it do that?

    It didn't do that because you haven't asked it to. After the call to SaveChanges(), EF doesn't have the data in the referenced row and it won't make a potentially redundant database call to get it.

    Calling db.Clients.Find(... tells EF to go and fetch the row from the database, which is why it returns the object.

提交回复
热议问题