2016-08-22 7 views
0

私は別のテーブルにAspNetUser(アイデンティティフレームワーク)にユーザデータを追加することができるソリューションを実現しようとしています。ユーザーが登録してログインすると、この新しいテーブルからデータを取得するだけです。ですから、私はApplicationUserと1対1の関係を作っています。カスタムDbを持つ統合アイデンティティフレーム

public class ApplicationUser : IdentityUser 
{ 
    //[ForeignKey("User")] 
    public Guid UserId { get; set; } 

    [Required] 
    public User User { get; set; } 

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
    { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     // Add custom user claims here 
     return userIdentity; 
    } 
} 


public class User 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public Guid UserId { get; set; } 
    public DateTime BirthDate { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Gender { get; set; } 

    public ApplicationUser AspNetUser { get; set; } 
} 

そして私は

public class MainDbContext: DbContext 
{ 
    public DbSet<Project> Projects { get; set; } 
    public DbSet<ProjectType> ProjectTypes { get; set; } 
    public DbSet<User> Users { get; set; } 

    public MainDbContext() 
     :base("MainDb") 
    { 

    } 
} 

2 Dbのコンテキストを持っており、この1つはアイデンティティフレームワークの私の状況です。

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
     public ApplicationDbContext() 
      : base("MainDb", throwIfV1Schema: false) 
     { 
     } 

     public static ApplicationDbContext Create() 
     { 
      return new ApplicationDbContext(); 
     } 
} 
+0

なぜ2つのデータベースが必要ですか?あなたの 'MainDbContext'が' IdentityDbContext'を継承することはできませんか?私が知る限り、DbContextのオブジェクトに、同じコンテキストにないオブジェクトに対するナビゲーションプロパティを持たせることはできません。 – Alisson

+0

データベースは1つだけですが、2つのコンテキストがあります。どのように私はそれを働かせることができます、私はIDのフレームワークで動作するデータベースにカスタムテーブルを追加する必要があります。どのように継承させることができるのかを説明してください。つまり、dbがアイデンティティーフレームワークテーブルを作成するときに、カスタムテーブルも作成されます。 – Letoir

+0

'ApplicationDbContext'と同じように、私は正しく答えるつもりです。 – Alisson

答えて

0

2つのDbContextをまったく使用する必要はありません。 (あなたが本当にこれを望んでいた場合を除き)

public class MainDbContext: IdentityDbContext<User> 
{ 
    public DbSet<Project> Projects { get; set; } 
    public DbSet<ProjectType> ProjectTypes { get; set; } 

    public MainDbContext() 
     :base("MainDb") 
    { 

    } 
} 

ます。また、カスタムプロパティ用に別のクラスを作成する必要はありません:あなたはこのように、あなたの主なDbContextはIdentityDbContextを継承することができます。ここで注意取る

public class User : IdentityUser 
{ 
    public DateTime BirthDate { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Gender { get; set; } 

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager) 
    { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     // Add custom user claims here 
     return userIdentity; 
    } 
} 

2つのこと:

  • IdentityUser親クラスがすでにそれを宣言するので、あなたは、ユーザーのID列を必要としないあなたは、このように行うことができます。
  • IdentityDbContextが既に宣言しているため、DbSetをUserに設定する必要はありません。

これは最も簡単なシナリオです。カスタムプロパティ用に別のクラスを作成し、DbContextsに分けることも可能ですが、これは複雑なシナリオのためのものです。

関連する問題