"The entity type 'CustomAttributeData' requires a primary key to be defined" error when I add-migration in EF Core

asp.net-core c# ef-code-first entity-framework-core

Question

I put my models in a class library with Core. I already did the first migration to test a part of my model, but after a big enhancement, I deleted my database and migration to have only one V1 migration.

The thing is, after this deletion, when I try to add-migration I get this error:

System.InvalidOperationException: The entity type 'CustomAttributeData' requires a primary key to be defined.
   at Microsoft.EntityFrameworkCore.Internal.ModelValidator.ShowError(String message)
   at Microsoft.EntityFrameworkCore.Internal.ModelValidator.Validate(IModel model)
   at Microsoft.EntityFrameworkCore.Internal.RelationalModelValidator.Validate(IModel model)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The entity type 'CustomAttributeData' requires a primary key to be defined.

I don't know why I am getting this error. All my models have a primary key defined with DataAnnotation.

If needed, I can provide the new models.

Here's the context if is related/helpful:

public class AmcContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Location> Location { get; set; }
    public DbSet<Rating> Rating { get; set; }
    public DbSet<RatingType> RatingType { get; set; }
    public DbSet<Subscription> Subscription { get; set; }
    public DbSet<Module> Module { get; set; }

    public AmcContext(): base()
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Ignore<User>();
        modelBuilder.Entity<Client>().ToTable("Client");
        modelBuilder.Entity<Professionnal>().ToTable("Professional");

        base.OnModelCreating(modelBuilder);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=localhost\SQLEXPRESS;Database=Askmycar;Trusted_Connection=True;");

        base.OnConfiguring(optionsBuilder);
    }
}

Thanks.

1
1
9/23/2019 4:46:24 PM

Accepted Answer

On one of my model I have a property like that :

public Type AvailableFor { get; set; } 

Type is an object so it was waiting for a primary key I change it to :

public string AvailableFor { get; set; } 

and its works.

Thanks to @H.Herzi :)

6
12/25/2016 11:00:20 AM

Popular Answer

I actually wanted to use Type in my model, and I had already initialized the properties with a converter:

modelBuilder.Entity<...>()
    .Property(x => x.Type)
    .IsRequired()
    .HasConversion(
        convertToProviderExpression: x => x.AssemblyQualifiedName,
        convertFromProviderExpression: x => Type.GetType(x));

I assumed this would've been enough for Entity Framework Core to realize that Type is not an entity, but I ended up having to explicitly ignore it for this to work:

modelBuilder.Ignore<Type>();


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow