我在使用 POCO 的实体框架方面遇到了一些问题,我希望有人能从高层次上告诉我我所看到的行为是否是预期的,或者我需要更深入地了解它发生的原因。
我有一个 classCustomer和 another CustomerType,所以Customer有一个属性Type(类型CustomerType指示类型)和CustomerType属性Customers是Customers 的集合(所有Customer具有该类型的 s)所以这些基本上是关联两端的 Navigation 属性,结果在 POCO 代码中类似于:
public partial class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int TypeId { get; set; }
public CustomerType Type { get; set; }
}
public partial class CustomerType
{
public CustomerType()
{
this.Customers = new HashSet<CustomerType>();
}
public int Id { get; set; }
public string TypeName { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
}
我已经关闭了 Proxy 创建和 LazyLoading(即两者DbContext.Configuration.ProxyCreationEnabled=false和DbContext.Configuration.LazyLoadingEnabled=false),因为它们使序列化变得很痛苦。
正如预期的那样,当我从Customer集合中获取实例时,Type它们的属性默认为 null。
但是,如果我从Customer集合中获取实例,.Include("Type")它不仅会加载Type属性,还会加载子级 - 即Customer每个这些上的 s 集合。
这是预期的吗?