为了进一步抽象我的存储库层,我尝试遵循此处描述的代码优先方法:http://msdn.microsoft.com/en-us/magazine/ee236639.aspx 。
我的帐户和订阅实体之间存在多对多关系。每个实体上都存在指向另一个实体的导航属性(例如 Account.Subscriptions)。
在创建自己的模型之前,我使用的是实体生成的模型,并且以下工作正常(“db”是实体上下文):
public IQueryable<Account> GetBySubscriptionId(int subId)
{
return from a in db.Accounts
where a.Subscriptions.Any(s => s.SubscriptionId == subId)
select a;
}
现在帐户的模型如下所示:
public class Account
{
public int AccountId { get; set; }
public string Name { get; set; }
// nav properties
public virtual List<Subscription> Subscriptions { get; set; }
}
当我尝试运行相同的 LINQ 查询时,我收到此错误:
"The specified type member 'Subscriptions' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."
非常感谢任何建议。
最佳答案
尝试更改签名
// nav properties
public virtual List<Subscription> Subscriptions { get; set; }
到
// nav properties
public virtual ICollection<Subscription> Subscriptions { get; set; }
无耻地从 Scott Hanselmann 的演示中窃取 - http://www.hanselman.com/blog/SimpleCodeFirstWithEntityFramework4MagicUnicornFeatureCTP4.aspx它使用这种模式,这里还有一个使用相同想法的 Scott Guthrie 演示 http://weblogs.asp.net/scottgu/archive/2010/07/23/entity-framework-4-code-first-custom-database-schema-mapping.aspx .
List<T>
是各种接口(interface)(ICollection、IQueryable、IEnumerable 等)的具体实现, Entity Framework 在从数据库检索内容时使用代理对象,因此 virtual
声明,它们使用这些接口(interface)的不同实现,这就是错误的来源。
关于linq - 代码第一次接近错误: The specified type member 'xxxxx' is not supported in LINQ to Entities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4299853/