C# 始终加载实体框架6 1-1导航属性

C# 始终加载实体框架6 1-1导航属性,c#,entity-framework,one-to-one,navigation-properties,C#,Entity Framework,One To One,Navigation Properties,我有一个名为Contact的实体类: public class Contact{ public int idContact { get; set; } public int id_Company { get; set; } public string Name { get; set; } public string Address { get; set; } public string Phone { get; set; } public str

我有一个名为Contact的实体类:

public class Contact{

    public int idContact { get; set; }
    public int id_Company { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string Phone { get; set; }
    public string Fax { get; set; }
    public string Mobile { get; set; }
    public string Email { get; set; }
    public bool ShippingLocation { get; set; }

    public virtual ICollection<Change> Changes { get; set; }
    public virtual ICollection<Entity> Entities { get; set; }
    public virtual ICollection<Entity> Entities1 { get; set; }
    public virtual Company Company { get; set; }
}

因此,理论上,当我访问公司属性时,我应该得到一个
ObjectDisposedException
,但我没有

您是如何知道自动加载的
的?不,您不了解延迟加载。当您通过
Include()
指定要加载的内容时,您正在进行快速加载。延迟加载是指当您不调用
include
时,它会在第一次访问时自动加载实体。当我使用调试器查看时,会填充
Company
对象。调试器还会触发延迟加载,您可以在检查/悬停实例/属性时使用探查器进行检查,探查器将显示对数据库的请求在我的情况下,每当我使用调试器查看时,上下文总是被释放。基本上,我使用块获取所有数据。
public Dictionary<int, Contact> GetContacts(Func<T, TKey> selector)
    {
        using (DbContext db = new Context())
        {
          return context.Set<Contact>().ToDictionary(selector);
        }
    }