SOLVED: ASP.NET Identity 2 Throws “EntityType 'IdentityUserLogin' has no key defined” and friends

I just got double tricked by an website error but if you are seeing this error along with a couple more similar ones then I can help to make them go away…


I loaded up a side-project that I had been working on and when I tried to run it the login screen displayed fine but then when I attempted to log in the following exception was thrown:

Exception Details: System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:
BrayfordNumbersPortal.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
BrayfordNumbersPortal.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.


The reason I got double tricked was because I went to the online version of the site and tested the login there but still got an error screen. This is a cautionary tale of not jumping to conclusions because I was then sent off down the rabbit hole wondering how the hell the site could have broken with the same error without me making any changes to the live version.

Because I had custom errors turned on I just saw the error screen and assumed that the local and remote sites were experiencing the same issue. That was the jumped to conclusion.


After some digging I came across a codeplex issue report that jogged my memory as to what the real issue was.

If you try to add some extra EF configuration by overriding `OnModelCreating` then you will see this issue.

To solve it you need to be sure to call the base `OnModelCreating` after your setup (or perhaps before if this doesn’t work – I’m not sure if it makes a difference either way).

My site is based on the ASP.NET Identity Sample (currently an alpha1 pre release) nuget package so in my case it is in a file called `/Models/IdentityModels.cs` but it could be in any class you have added it to, so just do a search for `OnModelCreating` and you should find the offending method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    // some custom setup code added by yourself – this bit could be anything
    modelBuilder.Entity<Course>().HasRequired(t => t.Department).WithMany(t => t.Courses).HasForeignKey(d => d.DepartmentID).WillCascadeOnDelete(false);

    // the all important base class call! Add this line to make your problems go away.

I quick recompile and this exception will be in your past.

Unfortunately for myself I then realised that it was now Dec (month 12) and the online site exception hidden behind the custom errors was actually the result of me incorrectly adding + 1 onto `today.month`. This will be the subject of the next post once I solve the issue…

1 comment :

Unknown said...

Saved my day !
Thank you :)