Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The entity type 'Configuration' requires a primary key to be defined

When I try to use Add-Migration I get this error:

The entity type 'Configuration' requires a primary key to be defined.

Now, I know that entities need keys, and it has one, but so far, simply decorating a property with [Key] did the job, but it doesn't seem to be so anymore. So i have the following entity:

public class Configuration
{
    [Key, ForeignKey("Client")]
    public int ClientId { get; set; }

    public CommunicationType CommunicationType { get; set; }

    public string CommunicationValue { get; set; }

    public virtual Client Client { get; set; }
}

But after searching for a while, I found out that apparently EF7 doesn't like it's conventions to be breached, and I need to rename ClientId to ConfigurationId, but that seems wrong to my coding conventions. Do i have to change my ways or is there anyway to bypass this? Thanks in advance.


edit

Here's the Client entity, and yes, there's a Configuration property there

public class Client
{
    [Key]
    public int ClientId { get; set; }

    public string Name { get; set; }

    public virtual User User { get; set; }

    public virtual List<Station> Stations { get; set; }

    public Configuration Configuration { get; set; }
}

edit

Full error log:

System.InvalidOperationException: The entity type 'Configuration' requires a primary key to be defined.
   in Microsoft.EntityFrameworkCore.Internal.ModelValidator.ShowError(String message)
   in Microsoft.EntityFrameworkCore.Internal.ModelValidator.EnsureNonNullPrimaryKeys(IModel model)
   in Microsoft.EntityFrameworkCore.Internal.ModelValidator.Validate(IModel model)
   in Microsoft.EntityFrameworkCore.Internal.RelationalModelValidator.Validate(IModel model)
   in Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   in Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.c__DisplayClass14_0.b__0(Object k)
   in System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   in Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   in Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   in Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
   in Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   in Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServiceCollectionExtensions.c.b__0_6(IServiceProvider p)
   in Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactoryService(FactoryService factoryService, ServiceProvider provider)
   in Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   in Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
   in Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   in Microsoft.Extensions.DependencyInjection.ServiceProvider.c__DisplayClass16_0.b__0(ServiceProvider provider)
   in Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   in Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
   in Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.c__DisplayClass6_0.b__9(IServiceProvider _)
   in Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactoryService(FactoryService factoryService, ServiceProvider provider)
   in Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   in Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
   in Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   in Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
   in Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   in Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
   in Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   in Microsoft.Extensions.DependencyInjection.ServiceProvider.c__DisplayClass16_0.b__0(ServiceProvider provider)
   in Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   in Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   in Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   in Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   in Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
   in Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.c__DisplayClass0_1.b__0()
   in Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.c__DisplayClass3_0`1.b__0()
   in Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The entity type 'Configuration' requires a primary key to be defined.
like image 275
Guilherme Lofrano Corneto Avatar asked Jan 31 '17 18:01

Guilherme Lofrano Corneto


People also ask

Does an entity require a primary key?

The primary key is an attribute or a set of attributes that uniquely identify a specific instance of an entity. Every entity in the data model must have a primary key whose values uniquely identify instances of the entity.

What is entity type configuration?

Allows configuration to be performed for an entity type in a model. An EntityTypeConfiguration can be obtained via the Entity method on DbModelBuilder or a custom type derived from EntityTypeConfiguration can be registered via the Configurations property on DbModelBuilder.

How do you define a key for an entity?

An entity key is a property or a set of properties of an entity type that are used to determine identity. The properties that make up an entity key are chosen at design time. The values of entity key properties must uniquely identify an entity type instance within an entity set at run time.

What is meant by primary key in Entity Framework?

Configuring a primary key By convention, a property named Id or <type name>Id will be configured as the primary key of an entity. Owned entity types use different rules to define keys. You can configure a single property to be the primary key of an entity as follows: Data Annotations.


1 Answers

Have you tried moving the ForeignKey attribute to the navigation property?

public class Configuration
{
    [Key]
    public int ClientId { get; set; }

    public CommunicationType CommunicationType { get; set; }

    public string CommunicationValue { get; set; }

    [ForeignKey("ClientId")]
    public virtual Client Client { get; set; }
}
like image 121
SBFrancies Avatar answered Sep 24 '22 04:09

SBFrancies