Asp.net Identity Validation Error

后端 未结 3 1099
闹比i
闹比i 2020-12-02 13:04

I want integrate IdentityContext with mydbcontext but i am taking this error

One or more validation errors were detected during model generation:

Ivdb.Dal.Co

相关标签:
3条回答
  • 2020-12-02 13:30

    If you don't want to call base.OnModelCreating and want to do your own mapping, your mapping should look like this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id).Property(p => p.Name).IsRequired();
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
        modelBuilder.Entity<IdentityUserLogin>().HasKey(u => new {u.UserId, u.LoginProvider, u.ProviderKey});
    }
    

    If you put the key for IdentityUserLogin only on UserId, you get DbEntityValidationExceptions when using the default google login.

    0 讨论(0)
  • 2020-12-02 13:37

    The take away is that you cannot have an empty OnModelCreating

    Good

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

    Bad

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      // base.OnModelCreating(modelBuilder);
      // your stuff here
    }
    
    0 讨论(0)
  • 2020-12-02 13:41

    Your code does't show this, but from the errors you are getting I assume that you are overriding OnModelCreating.This is where IdentityDbContext<ApplicationUser> configure the entity framework mappings. This means that if you want to override OnModelCreating you need to either call the base or you must do the mapping yourself.

    So either this:

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

    Or you do the mapping:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
    }
    
    0 讨论(0)
提交回复
热议问题