title: Error: The entity type requires a primary key to be defined
body: <p>I'm trying to follow some tutorial on Entity Framework Core. This tutorial takes the code-first approach on creating the database. When I'm trying to run the first migration, this error shows:</p>
  
  <blockquote>
    <p>The entity type 'Value' requires a primary key to be defined.</p>
  </blockquote>
  
  <p>The exact message is as follows:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: The entity type 'Value' 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.SqliteModelValidator.Validate(IModel
    model)    at
    Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext
    context, IConventionSetBuilder conventionSetBuilder, IModelValidator
    validator)    at
    Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.&lt;>c__DisplayClass5_0.b__1()
    at System.Lazy<code>1.ViaFactory(LazyThreadSafetyMode mode)    at
    System.Lazy</code>1.ExecutionAndPublication(LazyHelper
    executionAndPublication, Boolean useDefaultConstructor)    at
    System.Lazy<code>1.CreateValue()    at
    Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext
    context, IConventionSetBuilder conventionSetBuilder, IModelValidator
    validator)    at
    Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
    at
    Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()<br>
    at
    Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.&lt;&gt;c.&lt;TryAddCoreServices&gt;b__7_1(IServiceProvider
    p)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite
    factoryCallSite, ServiceProviderEngineScope scope)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor</code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite
    scopedCallSite, ServiceProviderEngineScope scope)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite
    constructorCallSite, ServiceProviderEngineScope scope)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor</code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite
    scopedCallSite, ServiceProviderEngineScope scope)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.&lt;&gt;c__DisplayClass1_0.&lt;RealizeService&gt;b__0(ServiceProviderEngineScope
    scope)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type
    serviceType, ServiceProviderEngineScope serviceProviderEngineScope)<br>
    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.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.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure&lt;System.IServiceProvider&gt;.get_Instance()
    at
    Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure</code>1
    accessor)    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.AddMigration.&lt;>c__DisplayClass0_1.&lt;.ctor>b__0()
    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 'Value' requires a primary key to be defined.</p>
  </blockquote>
  
  <p>(could someone edit this msg because it looks horrible and I'm not able to fix it?)</p>
  
  <p>I've double checked if I have <strong>Id</strong> property in the <strong>Value</strong> class and I've added [Key] to it just to be sure. 
  I've also restarted my laptop as it could have to do with an instance of server running from previous testing sessions or something like this.</p>
  
  <p>My model class:</p>
  
  <pre><code>using System.ComponentModel.DataAnnotations;
  
  namespace DatingApp.API.Models
  {
      public class Value
      {
          [Key]
          public int Id { get; set; }
          public string Name { get; set; } 
      }
  }
  </code></pre>
  
  <p>DbContext: </p>
  
  <pre><code>using DatingApp.API.Models;
  using Microsoft.EntityFrameworkCore;
  
  namespace DatingApp.API.Data
  {
      public class DataContext : DbContext
      {
          public DataContext(DbContextOptions&lt;DataContext&gt; options) : base (options) {}
  
          public DbSet&lt;Value&gt; Values {get; set;}
      }
  }
  </code></pre>
  
  <p>Startup.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using DatingApp.API.Data;
  using Microsoft.AspNetCore.Builder;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.AspNetCore.HttpsPolicy;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.EntityFrameworkCore;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Logging;
  using Microsoft.Extensions.Options;
  
  namespace DatingApp.API
  {
      public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
  
          }
  
          public IConfiguration Configuration { get; }
  
          // This method gets called by the runtime. Use this method to add services to the container.
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddDbContext&lt;DataContext&gt;(c =&gt; c.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
              services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
              else
              {
                 // app.UseHsts();
              }
  
              // app.UseHttpsRedirection();
              app.UseMvc();
          }
      }
  }
  </code></pre>
  
  <p>And connection string:</p>
  
  <pre><code>{
    "ConnectionStrings":
    {
      "DefaultConnection": "Data Source=DatingApp.db"
    },
    "Logging": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "AllowedHosts": "*"
  }
  </code></pre>
  
  <p>The thing I wrote in the terminal:</p>
  
  <pre><code>dotnet ef migrations add InitialMigration
  </code></pre>
  
  <p>EDIT: </p>
  
  <blockquote>
  <pre><code>D:\DatingApp\DatingApp.API&gt;dotnet ef migrations add InitialMigration -v
  Using project 'D:\DatingApp\DatingApp.API\DatingApp.API.csproj'.
  Using startup project 'D:\DatingApp\DatingApp.API\DatingApp.API.csproj'.
  Writing 'D:\DatingApp\DatingApp.API\obj\DatingApp.API.csproj.EntityFrameworkCore.targets'...
  dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\B590\AppData\Local\Temp\tmp24D5.tmp
  </code></pre>
    
    <p>/verbosity:quiet /nologo
    D:\DatingApp\DatingApp.API\DatingApp.API.csproj
        Writing 'D:\DatingApp\DatingApp.API\obj\DatingApp.API.csproj.EntityFrameworkCore.targets'...
        dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\B590\AppData\Local\Temp\tmp2B8C.tmp
    /verbosity:quiet /nologo
    D:\DatingApp\DatingApp.API\DatingApp.API.csproj
        dotnet build D:\DatingApp\DatingApp.API\DatingApp.API.csproj /verbosity:quiet /nologo</p>
  
  <pre><code>Compilation succesfull.
      Warnings: 0
      Errors: 0
  
  Time passed: 00:00:02.25
  dotnet exec --depsfile D:\DatingApp\DatingApp.API\bin\Debug\netcoreapp2.1\DatingApp.API.deps.json
  </code></pre>
    
    <p>--additionalprobingpath C:\Users\B590.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig
    D:\DatingApp\DatingApp.API\bin\Debug\netcoreapp2.1\DatingApp.API.runtimeconfig.json
    "C:\Program
    Files\dotnet\sdk\2.1.403\DotnetTools\dotnet-ef\2.1.4\tools\netcoreapp2.1\any\tools\netcoreapp2.0\any\ef.dll"
    migrations add InitialMigration --assembly
    D:\DatingApp\DatingApp.API\bin\Debug\netcoreapp2.1\DatingApp.API.dll
    --startup-assembly D:\DatingApp\DatingApp.API\bin\Debug\netcoreapp2.1\DatingApp.API.dll
    --project-dir D:\DatingApp\DatingApp.API\ --language C# --working-dir D:\DatingApp\DatingApp.API --verbose --root-namespace DatingApp.API
        Using assembly 'DatingApp.API'.
        Using startup assembly 'DatingApp.API'.
        Using application base 'D:\DatingApp\DatingApp.API\bin\Debug\netcoreapp2.1'.
        Using working directory 'D:\DatingApp\DatingApp.API'.
        Using root namespace 'DatingApp.API'.
        Using project directory 'D:\DatingApp\DatingApp.API\'.
        Finding DbContext classes...
        Finding IDesignTimeDbContextFactory implementations...
        Finding application service provider...
        Finding IWebHost accessor...
        Using environment 'Development'.
        Using application service provider from IWebHost accessor on 'Program'.
        Found DbContext 'DataContext'.
        Finding DbContext classes in the project...
        Using context 'DataContext'.
        System.InvalidOperationException: The entity type 'Value' 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.SqliteModelValidator.Validate(IModel
    model)
           at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext
    context, IConventionSetBuilder conventionSetBuilder, IModelValidator
    validator)
           at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.&lt;>c__DisplayClass5_0.b__1()
           at System.Lazy<code>1.ViaFactory(LazyThreadSafetyMode mode)
           at System.Lazy</code>1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
           at System.Lazy<code>1.CreateValue()
           at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext
    context, IConventionSetBuilder conventionSetBuilder, IModelValidator
    validator)
           at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
           at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
           at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.&lt;&gt;c.&lt;TryAddCoreServices&gt;b__7_1(IServiceProvider
    p)
           at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite
    factoryCallSite, ServiceProviderEngineScope scope)
           at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor</code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)
           at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite
    scopedCallSite, ServiceProviderEngineScope scope)
           at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)
           at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite
    constructorCallSite, ServiceProviderEngineScope scope)
           at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor</code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)
           at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite
    scopedCallSite, ServiceProviderEngineScope scope)
           at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)
           at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.&lt;&gt;c__DisplayClass1_0.&lt;RealizeService&gt;b__0(ServiceProviderEngineScope
    scope)
           at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type
    serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
           at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.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()
           at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
           at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure&lt;System.IServiceProvider&gt;.get_Instance()
           at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure</code>1
    accessor)
           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.AddMigration.&lt;>c__DisplayClass0_1.&lt;.ctor>b__0()
           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 'Value' requires a primary key to be defined.</p>
  </blockquote>