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();
}
}
なぜ2つのデータベースが必要ですか?あなたの 'MainDbContext'が' IdentityDbContext'を継承することはできませんか?私が知る限り、DbContextのオブジェクトに、同じコンテキストにないオブジェクトに対するナビゲーションプロパティを持たせることはできません。 – Alisson
データベースは1つだけですが、2つのコンテキストがあります。どのように私はそれを働かせることができます、私はIDのフレームワークで動作するデータベースにカスタムテーブルを追加する必要があります。どのように継承させることができるのかを説明してください。つまり、dbがアイデンティティーフレームワークテーブルを作成するときに、カスタムテーブルも作成されます。 – Letoir
'ApplicationDbContext'と同じように、私は正しく答えるつもりです。 – Alisson