Reputation: 384
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.
Upvotes: 7
Views: 8871
Reputation: 4230
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; }
}
Upvotes: 5