gpt4 book ai didi

c# - 实体类型 'Configuration' 需要定义主键

转载 作者:太空狗 更新时间:2023-10-29 21:38:03 24 4
gpt4 key购买 nike

当我尝试使用 Add-Migration 时出现此错误:

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

现在,我知道实体需要键,它有一个,但到目前为止,用 [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; }
}

关于c# - 实体类型 'Configuration' 需要定义主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41964707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com