entity-framework - HasKey Throwing InvalidOperationException-这是Entity Framework Code First中的错误吗?

标签 entity-framework entity-framework-4.1 code-first

好的,我在这里盯着屏幕看了几个小时,不知道为什么会收到此错误。我在许多其他项目上都使用过代码优先,以前对此没有任何问题...

这是错误:

System.InvalidOperationException was unhandled by user code
  Message=The properties expression 'sci => sci.ShoppingCartItemId' is not valid. The expression should represent a property: C#: 't => t.MyProperty'  VB.Net: 'Function(t) t.MyProperty'. When specifying multiple properties use an anonymous type: C#: 't => new { t.MyProperty1, t.MyProperty2 }'  VB.Net: 'Function(t) New From { t.MyProperty1, t.MyProperty2 }'.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.ModelConfiguration.Utilities.ExpressionExtensions.GetSimplePropertyAccessList(LambdaExpression propertyAccessExpression)
       at System.Data.Entity.ModelConfiguration.EntityTypeConfiguration`1.HasKey[TKey](Expression`1 keyExpression)
       at BillingPlatform.DataLayer.BillingDb.OnModelCreating(DbModelBuilder modelBuilder) in [somepath]\BillingDb.cs:line 57
       at System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
       at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
       at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
  InnerException: 

这是引发错误的代码。第一行:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{                       
   modelBuilder.Entity<ShoppingCartItem>().HasKey(sci => sci.ShoppingCartItemId);
   modelBuilder.Entity<Product>().HasKey<Guid>(p => p.ProductId);
   modelBuilder.Entity<DependentItemType>().HasKey<Guid>(dit => dit.DependentItemTypeId);
   modelBuilder.Entity<ProductCategory>().HasKey<Guid>(pc => pc.ProductCategoryId);

   base.OnModelCreating(modelBuilder);
}

这只是ShoppingCartItem类作为引用:
namespace BillingPlatform.Libraries
{
    public class ShoppingCartItem
    {
        /// <summary>
        /// The unique identifier of this shopping cart item.
        /// </summary>        
        public Guid ShoppingCartItemId { get; set; }

        public Product Product { get; set; }

        public decimal Price { get; set; }

        public decimal Tax { get; set; }

        public Guid UserId { get; set; }

        public bool InCart { get; set; }

        public string ProductData { get; set; }

        public DependentItemType DependentItemType { get; set; }

        public string DependentItemId { get; set; }
    }
}

有谁知道为什么 Entity Framework 会引发此错误?我的lambda表达式:
modelBuilder.Entity<ShoppingCartItem>().HasKey(s => s.ShoppingCartItemId);

非常简单。我看不出有什么问题...谢谢您给予的任何帮助!

最佳答案

好的,问题是我的类(class)成员原本只是领域。代码优先使用属性。更改代码并重建之后,我仍然遇到相同的错误。但是一旦Visual Studio被迫推送更新的DLL,一切就可以正常工作了。

关于entity-framework - HasKey Throwing InvalidOperationException-这是Entity Framework Code First中的错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8960026/

相关文章:

entity-framework - 在 Entity Framework Core 中更改带有索引的列

c# - 如何向域实体注入(inject)服务以及如何持久化实体

c# - 可以制作一个 C# 属性,该属性是 Entity Framework 代码首次迁移获取的其他属性的组合

c# - SQL Server Compact Edition 并发/线程安全(即非线程安全)

c# - 无法安装 Entity Framework Power Tools

.net - Entity Framework 批量显式加载

asp.net - EF 4.1 N Tier ASP .Net 困惑

c# - linq to entities 的 linq 更新问题

entity-framework - Entity Framework CTP5 代码优先映射 - 同一个表中的外键

entity-framework-4 - Entity Framework 4 代码优先 : Where is my database?