現在我已經創建代理禁用:如何在導航屬性上只使用EntityFramework 5獲得一級深度?
context.Configuration.ProxyCreationEnabled = false;
我有像這樣(除去非相關領域)的數據模型:
public partial class Video
{
public int VideoID { get; set; }
public string Title { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
}
public partial class User
{
public User()
{
this.Videos = new HashSet<Video>();
}
public int UserID { get; set; }
public string Username { get; set; }
public virtual ICollection<Video> Videos { get; set; }
}
我使用的工作單元和信息庫模式加載我的數據像這樣,
獲取所有視頻的,包括用戶對象:
var videos = videoService
.Include(v => v.User)
.Get()
我正在使用automapper從數據模型映射到域模型(因此下面的屏幕截圖中的UI命名空間)。當我檢查視頻枚舉我回去,並期待在枚舉中的第一個項目,我去檢查用戶對象:
我期待這裏要充滿VideoModel數據(好的),只有它的單個UserModel實體被填充數據(ok),並且UserModel中的所有集合都是空的(這是壞的)。正如您在上面的第二個紅色框中看到的,視頻集合中填充了6個視頻。而在這些視頻中,用戶都是填充的。所以這基本上創建了一個非常大的對象圖。
1)使用包含它只能進入1級深度(IE不填寫Video.User.Videos)時,我可以做到這一點嗎?
2)爲什麼不ProxyCreationEnabled = false照顧這個?我是否期待太多?
p.s.我想避免使用automapper爲此創建客戶映射器。 p.p.s.我正在做數據庫第一,不是模型第一
我假設你想要的與Entity Framework中的[Include](http://msdn.microsoft.com/en-us/library/bb738708(v = vs.90).aspx)函數類似?對於較早的版本,這需要在外鍵表中填充數據。 (也只是意識到你可能已經在使用它)。 – gunr2171
是的,我正在使用它。我不明白爲什麼當我嘗試只包含parent.child實體時,它加載了parent.child.children實體。 –
你有沒有想過這個?我遇到了完全相同的問題 – jtate