Merge MyDbContext with IdentityDbContext

前端 未结 8 1863
天命终不由人
天命终不由人 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 22:56

    You may get the following message if you follow the above steps but can't work out how to provide the key information. The error you may receive is:

    IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. Context.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.

    Create the two following classes

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

    In the OnModelCreating method within your Applications DbContext add the two configurations outlined above to the model:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
    
            modelBuilder.Configurations.Add(new IdentityUserLoginConfiguration());
            modelBuilder.Configurations.Add(new IdentityUserRoleConfiguration());
    
        }
    

    This should now get rid of the error methods when your model is being created. It did for me.

提交回复
热议问题