[英]The entity type 'CustomAttributeData' requires a primary key to be defined
When I attempt to run my add migrations I get the following I recently updated from .net 5 to .net 6, BUT I have no entity named CustomAttributeData how does one find this value out of what has cause the error.当我尝试运行我的添加迁移时,我得到以下信息我最近从 .net 5 更新到 .net 6,但是我没有名为 CustomAttributeData 的实体如何从导致错误的原因中找到这个值。
I think it might be down to this I am using a table for custom fields and am using the Type property type.我认为这可能是因为我正在为自定义字段使用一个表并使用 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; }
}
But as you see even it has a key against it?但正如你所见,即使它也有反对它的钥匙?
System.InvalidOperationException: The entity type 'CustomAttributeData' requires a primary key to be defined. System.InvalidOperationException:实体类型“CustomAttributeData”需要定义主键。 If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'.如果您打算使用无键实体类型,请在“OnModelCreating”中调用“HasNoKey”。 For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943 .有关无键实体类型的更多信息,请参阅https://go.microsoft.com/fwlink/?linkid=2141943 。
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel model, IDiagnosticsLogger`1 logger)在 Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel model,IDiagnosticsLogger`1 记录器)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)在 Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model,IDiagnosticsLogger`1 记录器)
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)在 Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model,IDiagnosticsLogger`1 记录器)
at Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)在 Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerModelValidator.Validate(IModel model,IDiagnosticsLogger`1 记录器)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model, Boolean designTime, IDiagnosticsLogger`1 validationLogger)在 Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model、Boolean designTime、IDiagnosticsLogger`1 validationLogger)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime)在 Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext 上下文,ModelCreationDependencies modelCreationDependencies,Boolean designTime)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime)在 Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(布尔设计时间)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()在 Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.b__8_4(IServiceProvider p)在 Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.b__8_4(IServiceProvider p)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite,TArgument 参数)
Yes you are right the Type
property is the issue and since it is an object it requires a primary key.是的,您是对的, Type
属性是问题所在,因为它是 object,所以它需要一个主键。
To solve this issue you have to tell EF to ignore it for migration to work.要解决此问题,您必须告诉 EF 忽略它才能进行迁移。
modelBuilder.Ignore<Type>();
You can also change the Type
property to another type depending on what information you want store.您还可以根据要存储的信息将Type
属性更改为另一种类型。 I find myself use enum
frequently.我发现自己经常使用enum
。
Since you are dealing with custom fields, I can assume an enum example such as this:由于您正在处理自定义字段,因此我可以假设一个枚举示例,如下所示:
public enum PropertyType
{
date,
email,
number,
text,
//more
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.