【问题标题】:How to add relationship between tables in two different DbContext如何在两个不同的 DbContext 中添加表之间的关系
【发布时间】: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


【解决方案1】:

简答:实体框架目前不支持创建使用多个上下文的查询。

解决方法:请参阅this

【讨论】:

    猜你喜欢
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2014-03-12
    相关资源
    最近更新 更多