如何在Entity Framework Core中创建没有主键的表?

时间:2018-04-03 09:56:12

标签: c# asp.net-core ef-code-first entity-framework-core asp.net-core-2.0

我有一个旧数据库,其中有一些没有主键的表。现在我将我的旧项目移动到Asp.Net Core并使用Entity Framework Core 2.0.1 Code First方法。我已经创建了表的模型类并尝试使用Add-Migration添加迁移,但它给出了以下错误。我从技术上考虑它的预期行为,但是还有什么其他替代方法,以便我不需要修改现有的表模式。

  

System.InvalidOperationException:实体类型' TestTable'要求   要定义的主键。在   Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel   模特)   Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel   模特)   Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel   模特)   Microsoft.EntityFrameworkCore.Internal.SqlServerModelValidator.Validate(IModel   模特)   Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(的DbContext   context,IConventionSetBuilder conventionSetBuilder,IModelValidator   验证者)   System.Collections.Concurrent.ConcurrentDictionary 2.GetOrAdd(TKey key, Func 2 valueFactory)at   Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()   在   Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
  在   Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite   factoryCallSite,ServiceProvider provider)at   Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite(IServiceCallSite   callSite,TArgument argument)at   Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite   constructorCallSite,ServiceProvider provider)at   Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite(IServiceCallSite   callSite,TArgument argument)at   Microsoft.Extensions.DependencyInjection.ServiceProvider<> c__DisplayClass22_0.b__0(的ServiceProvider   提供者)   Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(类型   serviceType)at   Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(的IServiceProvider   provider,输入serviceType)at   Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService [T](的IServiceProvider   提供者)   Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
  在   Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()   在   Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService [TService](IInfrastructure 1 accessor) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func 1   工厂)   Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(字符串   contextType)at   Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(字符串   name,String outputDir,String contextType)at   Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(字符串   name,String outputDir,String contextType)at   Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase<> c__DisplayClass3_0`1.b__0()   在   Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(动作   action)实体类型' TestTable'需要一个主键   定义

模特课程:

public class TestTable
{
    [StringLength(255)]
    public string Col1 { get; set; }
    [Required]
    public int Col2 { get; set; }
    [Required]
    public int Col3 { get; set; }
    public DateTime? Col4 { get; set; }
    [Required]
    public int Col5 { get; set; }
    [Required]
    public int Col6 { get; set; }
    [Required]
    public int Col7 { get; set; }
}

我认为一个解决方案就是将自动增量标识列添加为PK。请建议一个良好和正确的方法来做到这一点?

[Key]
public int Id { get; set; }

1 个答案:

答案 0 :(得分:1)

在上下文的代码文件中:

protected override void OnModelCreating( DbModelBuilder model_builder )
{
    base.OnModelCreating( model_builder );
    model_builder.Entity<TestTable>().HasKey(
        t => new { t.Id }
    );
}

所以基本上你只需要指定哪些列可以是主键。