这是我能想到的最好的东西。请随意指出此方法可能出现的任何缺陷或陷阱。
我重命名了导航属性ContactsCollection,并使用InversePropertyAttribute维护对数据库中原始集合的引用。Contacts属性现在返回状态为Active的Contacts列表。
public class Account
{
public int Id { get; set; }
public string Name { get; set; }
public int ContactId { get; set; }
[InverseProperty("Contact")]
public virtual ICollection<Contact> ContactsCollection { get; set; }
[NotMapped]
public ICollection<Contact> Contacts
{
get
{
return ContactsCollection.Where(c => c.Status == Status.Active).ToList();
}
}
}
现在,我不必搜索代码库,也不必用另一个名称重命名Contacts的属性。
每当试图将新的“仅获取联系人”属性分配或添加到其中时,我也会遇到编译错误。我从未在非编译的razer视图中分配或添加集合。到目前为止,这种方法效果很好。