EF 6 - EntityType 在 运行 Enable-Migrations 时没有定义键

EF 6 - EntityType has no key defined when running Enable-Migrations

我正在尝试启动 EF6。并坚持这个错误:EntityType 'Task' 没有定义键。为此 EntityType 定义键。

我知道这是一个常见错误,Google 搜索会导致许多博客/文章解释如何修复未定义的 "key"。但是我无法解决。我的实体在一个单独的库中,我不愿意使用注释方法来解决这个问题。我计划保持我的实体库没有任何依赖性..

这是我的模式:

public class Task
    {

        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime Due { get; set; }
        public int Priority { get; set; }
    }

这是我的背景:

   public class TaskManagerContext : DbContext
    {
        public TaskManagerContext() : base("TMConnection") { 


        }

        public DbSet<Task> Tasks { get; set; }
    }

当我 运行 Enable-Migrations 我得到以下错误:

PM> Enable-Migrations -Force
Checking if the context targets an existing database...
System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

TaskManager.DataLayer.Task: : EntityType 'Task' has no key defined. Define the key for this EntityType.
Tasks: EntityType: EntitySet 'Tasks' is based on type 'Task' that has no keys defined.

   at System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()
   at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
   at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
   at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(String language, String rootNamespace)
   at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
One or more validation errors were detected during model generation:

TaskManager.DataLayer.Task: : EntityType 'Task' has no key defined. Define the key for this EntityType.
Tasks: EntityType: EntitySet 'Tasks' is based on type 'Task' that has no keys defined.

如@HenkHolterman 所述,这很可能是由于自定义 class 与 .Net Framework 中的 class 同名。 Entity Framework 不能保证 select 您的 Task class 而不是 System.Threading.Tasks.Task 除非您通过完整的命名空间引用专门引用它。