gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:22:49 25 4
gpt4 key购买 nike

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

这是错误:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com