Merge MyDbContext with IdentityDbContext

前端 未结 8 1858
天命终不由人
天命终不由人 2020-11-28 22:22

I have a MyDbContext in a separated Data Accass Layer class library project. And I have an ASP.NET MVC 5 project with a default IdentityDbContext. The two context use the sa

8条回答
  •  日久生厌
    2020-11-28 23:13

    This may be an old thread, but this article was very helpful in demonstrating how to get the solution to the above question working: http://blogs.msdn.com/b/webdev/archive/2014/03/20/test-announcing-rtm-of-asp-net-identity-2-0-0.aspx

    I did end having to include

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         base.OnModelCreating(modelBuilder);
         //....
    }
    

    because without it, I would get the following error:

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

    If I add these configurations as mentioned above:

    public class IdentityUserLoginConfiguration : EntityTypeConfiguration
    {
        public IdentityUserLoginConfiguration()
        {
            HasKey(iul => iul.UserId);
        }
    }
    
    public class IdentityUserRoleConfiguration : EntityTypeConfiguration
    {
        public IdentityUserRoleConfiguration()
        {
            HasKey(iur => iur.RoleId);
        }
    }
    

    it would create an additional foreign key called Application_User.

提交回复
热议问题