Entity framework HasKey抛出InvalidOperationException——这首先是实体框架代码中的错误吗?

Entity framework HasKey抛出InvalidOperationException——这首先是实体框架代码中的错误吗?,entity-framework,entity-framework-4.1,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 pro

好吧,我在这里盯着屏幕看了几个小时,不知道为什么会出现这个错误。我已经在许多其他项目上使用了代码,以前没有遇到过任何问题

以下是错误:

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);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{                       
modelBuilder.Entity().HasKey(sci=>sci.ShoppingCartItemId);
modelBuilder.Entity().HasKey(p=>p.ProductId);
modelBuilder.Entity().HasKey(dit=>dit.DependentItemTypeId);
modelBuilder.Entity().HasKey(pc=>pc.ProductCategoryId);
基于模型创建(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; }
    }
}
namespace BillingPlatform.Libraries
{
公共类购物车项目
{
/// 
///此购物车项目的唯一标识符。
///         
公共Guid ShoppingCartItemId{get;set;}
公共产品产品{get;set;}
公共十进制价格{get;set;}
公共十进制税{get;set;}
公共Guid用户标识{get;set;}
公共布尔值{get;set;}
公共字符串ProductData{get;set;}
公共DependentItemType DependentItemType{get;set;}
公共字符串依赖项mid{get;set;}
}
}
有人知道实体框架为什么会抛出这个错误吗?我的lambda表达式:

modelBuilder.Entity<ShoppingCartItem>().HasKey(s => s.ShoppingCartItemId);
modelBuilder.Entity().HasKey(s=>s.ShoppingCartItemId);
非常简单。我看不出会出什么问题。。。谢谢你能给予的任何帮助

好吧,问题是我的类成员最初只是字段。代码首先需要属性。在进行代码更改和重建之后,我仍然会遇到相同的错误。但是,一旦Visual Studio被迫推送更新的DLL,一切都正常。

这里不是这样(通过谷歌搜索登陆这里),但可能值得一提的是,如果
HasKey
使用定义自己字段名的匿名类型,也可能会发生这种异常:

HasKey(t => new { KeyField1 = t.KeyField1, KeyField2 = t.KeyField2 });
应固定为如下所示:

HasKey(t => new { t.KeyField1, t.KeyField2 });

如果
ShoppingCartItemId
不是一个属性,只是一个公共字段,但在您的示例中它是一个属性,那么我可以重新设置异常。我能想象的是,您有另一种类型,名为
ShoppingCartItem
。请确保您正在modelbuilder中使用
BillingPlatform.Libraries.ShoppingCartItem
。因为它应该有用。@nemesv嘿。谢谢你。你说得对。最初,ShoppingCartItemId是一个字段。这就是问题所在。不幸的是,即使在我更改了它之后,我也没有意识到VisualStudio在依赖项目中没有使用更新的DLL。再次感谢!希望将来我能更快地发现问题…!:(