HasKey抛出InvalidOperationException - 这是Entity Framework Code First中的错误吗?

Ayo*_*o I 10 entity-framework code-first 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: 
Run Code Online (Sandbox Code Playgroud)

这是抛出错误的代码.第一行:

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);
}
Run Code Online (Sandbox Code Playgroud)

这里只是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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

有谁理解为什么Entity Framework会抛出这个错误?我的lambda表达式:

modelBuilder.Entity<ShoppingCartItem>().HasKey(s => s.ShoppingCartItemId);
Run Code Online (Sandbox Code Playgroud)

非常简单.我不明白会出现什么问题......谢谢你能给予的任何帮助!

Ayo*_*o I 12

好吧问题是我的班级成员原本只是田野.Code First期望属性.在进行代码更改和重建之后,我仍然遇到同样的错误.但是一旦Visual Studio被迫推送更新的DLL,一切都运行良好.