繁体   English   中英

实体类型“CustomAttributeData”需要定义主键

[英]The entity type 'CustomAttributeData' requires a primary key to be defined

当我尝试运行我的添加迁移时,我得到以下信息我最近从 .net 5 更新到 .net 6,但是我没有名为 CustomAttributeData 的实体如何从导致错误的原因中找到这个值。

我认为这可能是因为我正在为自定义字段使用一个表并使用 Type 属性类型。

public  class CustomFields
{
    [Key]
    public int Id { get; set; }

    public int GroupId { get; set; }

    public string PropertyName { get; set; }
    // And this is its value
    public Type PropertyType { get; set; }
    public bool? isActive { get; set; }

    public bool IsRequired { get; set; }
    public int? MaxLength { get; set; }


} 

但正如你所见,即使它也有反对它的钥匙?

System.InvalidOperationException:实体类型“CustomAttributeData”需要定义主键。 如果您打算使用无键实体类型,请在“OnModelCreating”中调用“HasNoKey”。 有关无键实体类型的更多信息,请参阅https://go.microsoft.com/fwlink/?linkid=2141943

在 Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel model,IDiagnosticsLogger`1 记录器)

在 Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model,IDiagnosticsLogger`1 记录器)

在 Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model,IDiagnosticsLogger`1 记录器)

在 Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerModelValidator.Validate(IModel model,IDiagnosticsLogger`1 记录器)

在 Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model、Boolean designTime、IDiagnosticsLogger`1 validationLogger)

在 Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext 上下文,ModelCreationDependencies modelCreationDependencies,Boolean designTime)

在 Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(布尔设计时间)

在 Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()

在 Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.b__8_4(IServiceProvider p)

在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite,TArgument 参数)

是的,您是对的, Type属性是问题所在,因为它是 object,所以它需要一个主键。

要解决此问题,您必须告诉 EF 忽略它才能进行迁移。

modelBuilder.Ignore<Type>();

您还可以根据要存储的信息将Type属性更改为另一种类型。 我发现自己经常使用enum

由于您正在处理自定义字段,因此我可以假设一个枚举示例,如下所示:

public enum PropertyType
{
    date,
    email,
    number,
    text,
    //more
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM