HasKey引发InvalidOperationException -- 这是Entity Framework Code First中的一个错误吗?

10

好的,我一直盯着屏幕几个小时了,但是我不知道为什么会出现这个错误。我在其他项目中使用了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 class)的参考代码:

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);

这非常简单。我不知道哪里出了问题...感谢您能提供的任何帮助!


如果 ShoppingCartItemId 不是属性而只是公共字段,我可以重现异常,但在您的示例中它是一个属性。我能想象到您有另一个名为 ShoppingCartItem 的类型。请确保在您的模型构建器中使用 BillingPlatform.Libraries.ShoppingCartItem。因为这应该可以工作。 - nemesv
@nemesv 嗨。谢谢你,nemesv。你是正确的。最初,ShoppingCartItemId是一个字段。这就是问题所在。不幸的是,即使我已经更改了它,我也没有意识到Visual Studio没有使用依赖项目中更新的dll文件。再次感谢!希望将来我能更快地发现问题...!:( - Ayo I
2个回答

12

问题在于我的类成员最初只是字段。Code First希望使用属性。在进行代码更改并重新构建后,我仍然遇到了相同的错误。但一旦强制让Visual Studio推送更新的DLL,一切都正常工作。


3
这里并非如此(通过谷歌搜索到达),但值得一提的是,如果HasKey使用了定义了自己字段名称的匿名类型,则也可能出现此异常:
HasKey(t => new { KeyField1 = t.KeyField1, KeyField2 = t.KeyField2 });

应该修复为以下这样:
HasKey(t => new { t.KeyField1, t.KeyField2 });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接