linq - 代码第一次接近错误: The specified type member 'xxxxx' is not supported in LINQ to Entities

标签 linq entity-framework linq-to-entities code-first

为了进一步抽象我的存储库层,我尝试遵循此处描述的代码优先方法: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/

相关文章:

c# - 使用 Entity Framework 优化 LINQ 查询

c# - 将 linq 查询创建为字符串

c# - 如何根据当前日期时间发现财政年度?

c# - 在带有 Entity Framework 的 ViewModel 上使用 DependencyProperty

c# - 取消长时间运行的 Entity Framework 6 异步请求

entity-framework - Entity Framework - 派生实体的关联

linq - LINQ to Entities 不支持指定的类型成员 'Date'。仅初始值设定项、实体成员和实体导航属性

c# - 使用 Linq to Entities 的存储函数

sql - LINQ to Entity,在NOT IN表上联接

c# - 选择 -> 引用自身(当前创建的列表)