简体   繁体   中英

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.

I think it might be down to this I am using a table for custom fields and am using the Type property 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. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943 .

at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel model, IDiagnosticsLogger`1 logger)

at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)

at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)

at Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)

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

at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime)

at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime)

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

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

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)

Yes you are right the Type property is the issue and since it is an object it requires a primary key.

To solve this issue you have to tell EF to ignore it for migration to work.

modelBuilder.Ignore<Type>();

You can also change the Type property to another type depending on what information you want store. I find myself use enum frequently.

Since you are dealing with custom fields, I can assume an enum example such as this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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