5

2人のユーザーにロールとカスタムフィールドを追加するデータベースを作成するクラスがあります。私の問題は、[dbo]。[IdentityUsers]の代わりに[dbo]。[AspNetUsers]にデータを保存することです。両方のテーブルが作成されます。シードされると、データはAspNetUserに送られます。 Webサイトを立ち上げ、新しいユーザーを登録すると、データはIdentityUserに送られます。ここでIdentity(Microsoft.Owin.Security)ユーザーによるEntity Frameworkのシード

はマイグレーションクラスである:ここで

internal sealed class Configuration : DbMigrationsConfiguration<DatabaseContext> 
{ 
    public Configuration() 
    { 
     AutomaticMigrationsEnabled = true; 
    } 

    protected override void Seed(DatabaseContext context) 
    { 
     base.Seed(context); 

     var userStore = new UserStore<ApplicationUser>(); 
     var manager = new UserManager<ApplicationUser>(userStore); 

     var role = new IdentityUserRole { Role = new IdentityRole(Model.Roles.ADMINISTRATOR) }; 
     var user = new ApplicationUser() { UserName = "123123", Email = "[email protected]", Language = "en-US"}; 
     user.Roles.Add(role); 
     IdentityResult result = manager.Create(user, "123123"); 

     var role2 = new IdentityUserRole { Role = new IdentityRole(Model.Roles.NORMAL) }; 
     var user2 = new ApplicationUser() { UserName = "qweqwe", Email = "[email protected]", Language = "fr-CA" }; 
     user.Roles.Add(role2); 
     IdentityResult result2 = manager.Create(user2, "qweqwe"); 
    } 
} 

は、アイデンティティモデルにカスタムフィールドを定義するApplicationUserクラスです。

public class ApplicationUser : IdentityUser, ICurrentUser 
{ 
    public ApplicationUser() 
    { 
     Email = ""; 
     Language = ""; 
    } 
    public string UserId { 
     get { return base.Id; } 
     set{} 
    } 
    public string Email { get; set; } 
    public string Language { get; set; } 

} 

ここに、この新しいクラスのEntity Framework構成クラスがあります。

public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser> 
{ 
    public ApplicationUserConfiguration() 
    { 
     this.HasKey(d => d.Id); 
     this.Ignore(d => d.UserId); 
    } 
} 

enter image description here

同じ設定で保存したDataContextを使用している両方:私の質問は

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

     //... others entity here 

     modelBuilder.Configurations.Add(new ApplicationUserConfiguration()); 

     //Theses configuration are required since custom fields are added to ApplicationUser. Here is why : http://stackoverflow.com/questions/19474662/map-tables-using-fluent-api-in-asp-net-mvc5-ef6 
     modelBuilder.Entity<IdentityUserLogin>().HasKey(l => l.UserId); 
     modelBuilder.Entity<IdentityRole>().HasKey(r => r.Id); 
     modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId }); 

    } 

は以下のとおりです。なぜ私はアイデンティティテーブルから複製されたASPNET prefixeテーブル名を持っていますか?なぜWebアプリケーションがもう一方を使用している間に、シードは1つを使用しますか?

答えて

6

あなたのDbContextはIdentityDbContextから継承していますか?

IdentityDbContextから継承する場合は、OnModelCreatingに追加の設定は必要ありません。 IdentityDbContextとデフォルトのEntity Frameworkの規則によって提供されるマッピングで十分です。

あなたはユーザーIDのsetterなしであなたのApplicationUserを宣言した場合、あなたはユーザーIDプロパティを無視する必要はありません。

public class ApplicationUser : IdentityUser 
{ 
    public ApplicationUser() 
    { 
     Email = ""; 
     Language = ""; 
    } 

    public string Email { get; set; } 
    public string Language { get; set; } 

    public string UserId 
    { 
     get { return Id; } 
    } 
} 

次に、あなたのApplicationDbContextは、このような単純なことができます(私はあなたの設定の名前を変更しました明確にするためMigrationConfigurationのクラス):

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    static ApplicationDbContext() 
    { 
     Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, MigrationConfiguration>()); 
    } 

    public ApplicationDbContext() 
     : base("DefaultConnection") 
    { 
    } 
} 

予想通り、この作品(AspNetXXXXという名前のテーブルのみ、AspNetUsersテーブルは、カスタムフィールドを持つ)AspNet.Identity.Coreのリリースバージョン(バージョン1.0.0)とし、 AspNet.Identity。 EntityFrameworkアセンブリ enter image description here

+1

ありがとうございます。私はIdentityContextでそれを行い、すべてがうまくいった。 –

関連する問題