我正试图启动EF6。并坚持这个错误: EntityType'任务'没有定义键。定义此EntityType的密钥。
我知道这是一个常见的错误,谷歌搜索导致许多博客/文章解释如何修复未定义的"密钥" 。但我无法解决。我将我的实体放在一个单独的库中,我不愿意使用注释方法来解决这个问题。我打算保持我的实体库清除任何依赖..
这是我的模式:
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.
答案 0 :(得分:2)
如@HenkHolterman所述,这很可能是由于.Net Framework中有一个与类同名的自定义类。实体框架无法保证选择您的Task
类而不是System.Threading.Tasks.Task
,除非您通过完整的命名空间参考专门引用它。