Create ASP.NET Identity tables using SQL script

前端 未结 4 1467
借酒劲吻你
借酒劲吻你 2020-12-05 19:38

I am attempting to incorporate ASP.NET Identity into a new application that currently uses a SQL script to create the database schema. As we will need to create Foreign Key

4条回答
  •  旧时难觅i
    2020-12-05 20:22

    So the new 1.1-alpha1 bits will have something close to the following by default. This might be what you are looking for in regards to the Foreign Keys. Note: this is a bit different than 1.0 as the navigation properties changed a bit to enable the ability to specify the primary key type:

    We are trying to address some of the EF migration/extensibility issues, so things hopefully will be easier with Identity 1.1-alpha1 and the upcoming 6.0.2/6.1 EF releases, but I'm not sure the updated EF packages are available on myget yet.

        var user = modelBuilder.Entity()
            .ToTable("AspNetUsers");
        user.HasMany(u => u.Roles).WithRequired().HasForeignKey(ur => ur.UserId);
        user.HasMany(u => u.Claims).WithRequired().HasForeignKey(uc => uc.UserId);
        user.HasMany(u => u.Logins).WithRequired().HasForeignKey(ul => ul.UserId);
        user.Property(u => u.UserName).IsRequired();
    
        modelBuilder.Entity()
            .HasKey(r => new { r.UserId, r.RoleId })
            .ToTable("AspNetUserRoles");
    
        modelBuilder.Entity()
            .HasKey(l => new { l.UserId, l.LoginProvider, l.ProviderKey})
            .ToTable("AspNetUserLogins");
    
        modelBuilder.Entity()
            .ToTable("AspNetUserClaims");
    
        var role = modelBuilder.Entity()
            .ToTable("AspNetRoles");
        role.Property(r => r.Name).IsRequired();
        role.HasMany(r => r.Users).WithRequired().HasForeignKey(ur => ur.RoleId);
    

提交回复
热议问题