私はVisual StudioでデフォルトのASP.Net MVCテンプレートを使用しています。私はテンプレートで私のために作成されたASP.Net IDコードを使用しています。私は、私がApplicationUserエンティティ(AspNetUserテーブル)と他のエンティティとの関係を認識するために使用するDBContextを使用したいと思います。たとえば、ApplicationUserとMessageエンティティの間の関係を示すApplicationUser.Messagesプロパティを持つことができます。データアクセスレイヤープロジェクト内のすべての非Identityエンティティ用のDbContextを持っています。テンプレートApplicationDbContextはUIレイヤーにあります。 IDエンティティとカスタムエンティティの関係を維持するために、1つのDbContextにマージする必要があります。これはどうすればいいですか?ここでASP.Net ID DbContextとDbContextをマージする
は、私が持っているもののいくつかのサンプルコードです:
IdentityUserとDbContextは私のカスタムメッセージプロパティを持つMVCテンプレートからUIレイヤのプロジェクトで私のために作成:
public class ApplicationUser : IdentityUser
{
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 ICollection<Message> Messages { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Messageクラス
public class Message
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Body { get; set; }
[Required]
public DateTime Created { get; set; }
}
私は私のデータアクセス層のプロジェクトを持っているDBContext:私は私のドメイン/ビジネスロジック層を持っている
public class PSNContext : DbContext, IPSNContext
{
public PSNContext()
:base ("DefaultConnection")
{
}
public DbSet<Message> Messages { get; set; }
}
それは私のビジネスロジック層にUI層にApplicationUserからこのようなUI固有のコードを持参する権利を感じていない:
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
はこれを行うには良い方法はありますか?
[Merge MyDbContextとIdentityDbContextの可能な複製](http://stackoverflow.com/questions/19764233/merge-mydbcontext-with-identitydbcontext) – Arvand
質問は正しいです。私は同じ問題に直面しています。 UIレイヤーに1つ、DataAccessレイヤーに2つのDbContextを維持したくありません。 ASP.NETアイデンティティクラスをDataLayerに移動すると、System.AspNet.Identity名前空間もDataLayerに移動する必要があり、データレイヤーをUIテクノロジから独立させる必要があります。データレイヤーに名前空間 "AspNet"があるのは正しいですか? –