我有2個班是這樣的:C#EntityFramework 6.0 - 如何在EntityTypeConfiguration中使用Where語句?
Parent.cs
public class Parent
{
public int Id {get;set;}
public virtual ICollection<Child> Children { get; set; }
}
Child.cs
public class Child
{
public int Id {get;set;}
public ItemStatusType ItemStatusTyp { get; set; }
public int ParentId {get;set;}
[ForeignKey("ParentId")]
public virtual Parent Parent { get; set; }
}
ItemStatusType.cs
public enum ItemStatusType
{
Active = 1,
Deactive = 2,
Deleted = 3
}
我想被以某種方式獲取什麼永遠的有效個而不是刪除個。由於我沒有刪除物理記錄,我只是將ItemStatusType
更新爲Deleted
狀態。
所以,當我說ParentObj.Children
我只希望檢索活動的沒有進一步使用Where
條件。
這是迄今爲止我做了什麼,但給人的運行時異常,我後來說:
public class ParentConfiguration : EntityTypeConfiguration<Parent>
{
public ParentConfiguration()
{
HasMany(c => c.Children.Where(p => p.ItemStatusTyp != ItemStatusType.Deleted).ToList())
.WithRequired(c => c.Parent)
.HasForeignKey(c => c.ParentId)
;
}
}
運行時異常:
表達式「C => c.Children。 Where(p =>(Convert(p.ItemStatusTyp) != 3))。ToList()'不是有效的屬性表達式。表達式 應表示屬性:C#:'t => t.MyProperty'VB.Net: 'Function(t)t.MyProperty'。
我不得不在表達式後使用ToList
,否則它不會編譯。
什麼是正確的做什麼我想要什麼?
由於提前,
你不能那樣。想象一下,如何看待數據庫,這基本上是配置設置。 – DavidG
EF在這種對開發友好的糖果中一直很貧瘠。NHibernate功能非常豐富,如果不是EF的卓越LINQ支持,我幾乎可以切換。所以不,你不能用條件配置導航屬性。您可能需要使用提供全局過濾器的第三方庫或查找更復雜的方式來實施軟刪除。 –