title: How to create table without primary key in Entity Framework Core?
body: <p>I have an old database which has some tables without primary key. Now I'm moving my old project to Asp.Net Core and using Entity Framework Core 2.0.1 Code First approach. I have made the model class of table and trying to add migration using <code>Add-Migration</code>, but it gives the below error. I think technically its expected behavior, But what are other alternative ways so that I don't need to modify the existing table schema.</p>
  
  <blockquote>
    <p>System.InvalidOperationException: The entity type 'TestTable' requires
    a primary key to be defined.    at
    Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel
    model)    at
    Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel
    model)    at
    Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel
    model)    at
    Microsoft.EntityFrameworkCore.Internal.SqlServerModelValidator.Validate(IModel
    model)    at
    Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext
    context, IConventionSetBuilder conventionSetBuilder, IModelValidator
    validator)    at
    System.Collections.Concurrent.ConcurrentDictionary<code>2.GetOrAdd(TKey
    key, Func</code>2 valueFactory)    at
    Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
    at
    Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()<br>
    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite
    factoryCallSite, ServiceProvider provider)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite
    scopedCallSite, ServiceProvider provider)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor</code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite
    constructorCallSite, ServiceProvider provider)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite
    scopedCallSite, ServiceProvider provider)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor</code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)    at
    Microsoft.Extensions.DependencyInjection.ServiceProvider.&lt;>c__DisplayClass22_0.b__0(ServiceProvider
    provider)    at
    Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type
    serviceType)    at
    Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider
    provider, Type serviceType)    at
    Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider
    provider)    at
    Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()<br>
    at
    Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() 
    at
    Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure<code>1
    accessor)    at
    Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func</code>1
    factory)    at
    Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String
    contextType)    at
    Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String
    name, String outputDir, String contextType)    at
    Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String
    name, String outputDir, String contextType)    at
    Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.&lt;>c__DisplayClass3_0`1.b__0()
    at
    Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action
    action) The entity type 'TestTable' requires a primary key to be
    defined.</p>
  </blockquote>
  
  <p><strong>Model class:</strong></p>
  
  <pre><code>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; }
  }
  </code></pre>
  
  <p>I have thought one solution like to add an autoincrement identity column as PK. Please suggest a good and proper way to do this?</p>
  
  <pre><code>[Key]
  public int Id { get; set; }
  </code></pre>