[英]The entity type 'Configuration' requires a primary key to be defined
When I try to use Add-Migration
I get this error: 当我尝试使用Add-Migration
我收到此错误:
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. 现在,我知道实体需要键,它有一个,但到目前为止,简单地用[Key]
装饰一个属性完成了工作,但它似乎不再如此。 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. 但经过一段时间的搜索后,我发现显然EF7并不喜欢它违反的惯例,我需要将ClientId
重命名为ConfigurationId
,但这似乎对我的编码约定是错误的。 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 这是Client
实体,是的,那里有一个Configuration
属性
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.
Have you tried moving the ForeignKey attribute to the navigation property? 您是否尝试将ForeignKey属性移动到导航属性?
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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.