【发布时间】:2016-09-19 17:15:30
【问题描述】:
我有两个 DbContext 类和其中的一些实体。我知道拥有多个 DbContext 并不是一个好主意,但我必须做很多工作来更改我的代码!所以我的问题是在具有不同 DbContext 的两个实体之间添加关系的最佳方案是什么?
例如,带有 IdentityDb 上下文的用户实体和带有 SiteDbContext 的评论实体之间的一对多关系:
public class User : IdentityUser
{
public DateTime JoinDate { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
public class IdentityDb : IdentityDbContext<ApplicationUser>
{
public IdentityDb() : base("DefaultConnection", throwIfV1Schema: false)
{
}
public static IdentityDb Create()
{
return new IdentityDb();
}
}
public class Comment
{
public int CommentId { get; set; }
[Required]
[StringLength(900)]
public string CommentText { get; set; }
[DataType(DataType.Date)]
public DateTime CommentDate { get; set; }
}
public class SiteDb: DbContext
{
public SiteDb(): base("DefaultConnection")
{
}
public DbSet<Comment> Comments{ get; set; }
}
【问题讨论】:
标签: entity-framework