实体类型“配置”需要定义主键

时间:2017-01-31 18:17:44

标签: c# entity-framework entity-framework-core

当我尝试使用Add-Migration时,我收到此错误:

  

实体类型“配置”需要定义主键。

现在,我知道实体需要密钥,而且它有一个,但到目前为止,简单地用[Key]装饰一个属性完成了工作,但它似乎不再如此。所以我有以下实体:

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; }
}

但是在搜索了一段时间之后,我发现显然EF7不喜欢它违反的惯例,我需要将ClientId重命名为ConfigurationId,但这似乎对我的编码不对约定。我是否必须改变我的方式,或者无论如何都要绕过这个?提前谢谢。

修改

这是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; }
}

修改

完整错误日志:

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.

1 个答案:

答案 0 :(得分:5)

您是否尝试将ForeignKey属性移动到导航属性?

csv