我剛剛調用EF 4.0解決方案以使用EF 4.1 Code-First。一切順利,我能夠從數據庫獲取數據,並在幾個小時內使用WCF RIA Services 1.0SP2-RC將其顯示在我的Silverlight項目中。在RIA服務+實體框架代碼中加載協會首先
當我嘗試在我的域服務中加載引用時,會出現唯一的問題。我已將[Include]屬性添加到我的引用屬性中。另外,我已經啓用延遲加載我DbContext.But還在,下面的方法沒有返回值(以下上Ef中4.0的工作,但不是4.1):
public IEnumerable<Bar> GetBarsInFoo (int fooId, bool direction)
{
var foo = this.DbContext.Foos.FirstOrDefault(f => f.FooId == fooId);
var Bars = foo.Bars.Where(b => b.Direction == direction).ToList();
return Bars;
}
所以我要檢查,看看是否被引用的集合不包括,然後加載它:
public IEnumerable<Bar> GetBarsInFoo (int fooId, bool direction)
{
var foo = this.DbContext.Foos.FirstOrDefault(f => f.FooId == fooId);
// Additional code to load the relation.
if (!this.context.Entry(foo).Collection(f => f.Bars).IsLoaded)
{
this.context.Entry(foo).Collection(f => f.bars).Load();
}
var Bars = foo.Bars.Where(b => b.Direction == direction).ToList();
return Bars;
}
上面的代碼,正確返回結果。
問題是我必須改變我的代碼的很多部分,如果我想處理這個問題,正如我所提到的。
這裏是我的實體定義:
public partial class Foo
{
[Key]
public int FooId {get; set;}
[Include]
[Association("Foo_Bar", "FooId", "FooId")]
[Display(AutoGenerateField = false)]
public virtual ICollection<Bar> Bars { get; set; }
}
public partial class Bar
{
[Key]
public int BarId {get; set;}
[Display(AutoGenerateField = false)]
public int FooId { get; set; }
[Include]
[Display(AutoGenerateField = false, Name = "In Foo", Order = 12)]
[Association("Foo_Bar", "FooId", "FooId", IsForeignKey = true)]
[ForeignKey("FooId")]
public virtual Foo Foo { get; set; }
}
有什麼辦法,我可以讓EF手柄裝入關係沒有我檢查?