简体   繁体   中英

“Define the key for this EntityType.” when including attribute of type ApplicationUser

I'm writing C#/ASP.NET MVC5 web application, and I'm hitting the following errors when I try to do a new migration

ToTheMoon.DAL.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
ToTheMoon.DAL.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.

I have included a property(?) of type ApplicationUser in one of my models. I believe that this is what's causing it:

public ApplicationUser Requester { get; set; }

I am well aware of the fact that—in general—I should explicitly mark something as a key with the [Key] annotation, but I don't believe this applies as the error messages are describing classes that are part of MVC5's 'batteries included' login system.

I have seen something about this being caused by incorrectly overriding a method (can't remember what one it is now), but I'm not doing that, so all other questions that I've found online are useless to me.

Anything is appreciated.

EDIT: Here's my context, it's pretty bare.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DefaultConnection") { }
}

From the comments in the Question:

It appears the entity that added the ApplicationUser Requester property is defined in a different context. Because it is in a different context that context knows nothing about the configurations for all of the Identity entities, since that is set up by the IdentityDbContext.OnModelCreating method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM