EF 6-运行启用迁移时,EntityType没有定义键

阿卜杜勒·拉奥夫(Abdel Raoof)

我正在尝试启动EF6。并停留在此错误:EntityType'任务'没有定义键。定义此EntityType的键。

我了解这是一个常见错误,并且Google搜索导致许多博客/文章介绍了如何解决未定义的“键”。但是我无法解决。我的实体位于单独的库中,我不愿意使用注释方法来解决此问题。我计划使我的实体库远离任何依赖关系。

这是我的模式:

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所述,这很可能是由于具有与.Net Framework中的类同名的自定义类引起的。除非您通过完整的命名空间引用专门引用它,否则不能保证实体框架会选择您的TaskSystem.Threading.Tasks.Task

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

实体类型没有定义键EF6

来自分类Dev

EF 6如何在生成迁移时跳过现有表

来自分类Dev

EF6 OnModelCreating()事件没有

来自分类Dev

EF 6启用迁移找不到上下文

来自分类Dev

外键查找下拉列表EF 6

来自分类Dev

通过现有迁移从EF5迁移到EF6

来自分类Dev

EF 6带有交易的SaveChanges

来自分类Dev

使用Npgsql进行EF6迁移时出现DbProviderServices错误

来自分类Dev

带有自定义连接字符串的EF 6编程迁移

来自分类Dev

具有迁移功能的ASP.NET MVC 6 + EF6

来自分类Dev

使用EF6迁移失败重命名身份表

来自分类Dev

使用EF6迁移更改表之间的关系

来自分类Dev

EF 6基于代码的配置和迁移错误

来自分类Dev

迁移到MVC6 / EF7:PluralizingTableNameConvention

来自分类Dev

启用EF6 LazyLoadingEnabled,但加载相关对象

来自分类Dev

EF6,代码优先,启用迁移,“无法加载指定的元数据资源”

来自分类Dev

用EF 6过滤

来自分类Dev

查找并包含EF 6

来自分类Dev

刷新EF 6 DBContext

来自分类Dev

通过EF 6 / Code First键查找本地缓存的实体

来自分类Dev

EF6对外键的唯一约束

来自分类Dev

EF6 CF中的外键问题

来自分类Dev

EF 6保存相关实体组合键

来自分类Dev

EF6 ApplicationUser错误的多个外键

来自分类Dev

在EF6中更新外键对象

来自分类Dev

如何更改 MySQL EF 6 上的外键选项?

来自分类Dev

使用EF6和.NET 4.6.1进行数据库迁移时遇到多个错误

来自分类Dev

将EF6与自定义Api分离

来自分类Dev

我应该在没有唯一字段的类中定义什么键,以便它可以被EF 6使用?