joe_coolish
joe_coolish

Reputation: 7259

Entity Framework 6.1 Code First migration error: EntityType 'IdentityUserRole' has no key defined

I created a new MVC 5 project and I tried to enable migrations for the Code First entities I've been creating, when I got the following PS exceptions in the Package Manager Console:

One or more validation errors were detected during model generation:

Project.Web.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
Project.Web.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.

The problem is that there are no classes called IdentityUserRole or IdentityUserLogin in my project. There are classes with those names located in the Microsoft.AspNet.Identity.EntityFramework namespace, but the error is saying that those classes exist in my project's namespace.

How do I resolve this issue? I've cleaned and recompiled the project several times, and nothing seems to fix this issue.

EDIT

If I remove my OnModelCreating override, everything works... This is what I have in the override:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<CommercialEntity>().ToTable("Commercial", "Entity");
    modelBuilder.Entity<ResidentialEntity>().ToTable("Residential", "Entity");
    modelBuilder.Entity<ContactsList>().ToTable("ContactsList", "List");
    modelBuilder.Entity<ListFacet>().ToTable("ListFacet", "List");
}

I want to set the Schema for my tables in EF. Is there another way to achieve this that works with migrations?

Upvotes: 1

Views: 1181

Answers (1)

joe_coolish
joe_coolish

Reputation: 7259

I just found the answer! Playing around with the OnModelCreating override in my Db Context class, I realized I wasn't calling the base method. I added the following line at the end of the override and everything started working!

    base.OnModelCreating(modelBuilder);

Hope this helps someone else!

Upvotes: 5

Related Questions