實體類型“配置”需要定義主鍵

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.

熱門答案

您是否嘗試將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; }
}


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow