我在使用POCO的實體框架中遇到了一些問題,我希望有人可以在高層告訴我,如果我看到的行爲是預期的或者我需要深入研究它爲什麼會發生。禁用代理創建和延遲加載的實體框架仍在加載子對象
我有一個類Customer
和另一CustomerType
,所以Customer
具有(指示型的CustomerType
類型)的Type
財產和CustomerType
具有財產Customers
這是Customer
S(所有Customer
S作這種類型的),所以這些集合基本上上的關聯的兩端的導航屬性,導致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; }
}
我已經關閉代理創建和惰性加載(即兩個DbContext.Configuration.ProxyCreationEnabled=false
和DbContext.Configuration.LazyLoadingEnabled=false
),因爲它們使序列化變得很痛苦。
正如預期的那樣,當我從Customer
集合中獲取實例時,默認情況下它們的Type
屬性爲null。
但是,如果我從Customer
實例集合了.Include("Type")
,它不僅是加載Type
性能,但它也裝載了兒童 - 即Customer
S於每一種集合。
這是預期嗎?
我不知道你的意思是「MyClass」和「MyClassType」。也許你可以爲這兩個類編寫C#代碼,而不是試圖描述它。 – Aron 2013-05-14 02:52:59
對不起,命名很混亂 - 我的編輯更清楚了嗎?這些只是試圖說明這個概念的例子。 – mutex 2013-05-14 04:15:10
不...沒有幫助。 [看看這裏的例子,你應該如何描述你的類](http://stackoverflow.com/questions/16523686/unable-to-determine-the-principal-end-of-an-association)。我們是這裏的開發者,而不是英語專業。我們發現代碼比散文容易閱讀。 – Aron 2013-05-14 04:19:24