Abdel Raoof Olakara
Abdel Raoof Olakara

Reputation: 19353

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

I am trying to kickstart EF6. And stuck with this error: EntityType 'Task' has no key defined. Define the key for this EntityType.

I understand this is a common error and a Google search leads to many blogs / articles that explain how to fix a undefined "key" . But I am unable to solve. I have my entities in a separate library and I am not willing to use the annotation method to solve this. I plan to keep my entities library clean of any dependences..

Here is my mode:

public class Task
    {

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

And here is my context:

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


        }

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

When I run Enable-Migrations I get the following error:

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.

Upvotes: 2

Views: 1903

Answers (1)

Claies
Claies

Reputation: 22323

As stated by @HenkHolterman, This is most likely being caused by having a custom class with the same name as a class from the .Net Framework. Entity Framework cannot be guaranteed to select your Task class rather than System.Threading.Tasks.Task unless you Specifically refer to it by the full namespaced reference.

Upvotes: 3

Related Questions