EntityFramework核心如何在不创建联接表的情况下映射多对多 [英] Entityframework core how to map Many-to-Many without creating a join table

查看:100
本文介绍了EntityFramework核心如何在不创建联接表的情况下映射多对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此活动从现有的.edmx项目中克隆以首先进行编码.

I am trying this activity to clone from existing .edmx project to code first.

我有两个实体.我想建立多对多关系而不创建新表.我正在使用EF Core 2.0代码优先方法.

I have two entities. I want to have many to many relation without creating a new table. I am using EF Core 2.0 code first approach.

请在下面找到我创建的实体.我不确定这是否是正确的方法.

Please find below the entity i have created. I am not sure whether is this is the right way to do this.

我想在两个表上都具有外键列,即.分别在WorkCase和Workflow表上的WorkflowId和WorkCaseId.

I would like to have the foreign key column on both the tables ie. WorkflowId and WorkCaseId on WorkCase and Workflow tables respectively.

    public class WorkCase
    {
        [Key]
        public int WorkCaseId { get; set; }

        public int WorkflowId { get; set; }

        public int CaseDetailId {get;set;}

        public CaseDetail CaseDetail {get;set;}

        public WorkFlow WorkFlow { get; set; }

        public ICollection<WorkFlow> WorkFlows { get; set; }
    }

    public class WorkFlow : BaseEntity
    {
        [Key]
        public int WorkFlowId { get; set; }

        public string Comment { get; set; }

        public DateTime? UpdateDate { get; set; }

        public int WorkCaseId { get; set; }

        public WorkCase WorkCase { get; set; }

        public ICollection<WorkCase> WorkCases { get; set; }
    }

我的期望如下.任何人都可以帮助实现EF Core配置的方法:-工作区将具有最新的工作流ID-工作流将具有每个工作用例ID的历史记录.

My expectation is as below. Can anyone help how to achieve the EF Core configuration: - Workcase will have the latest workflowid - workflow will have history for each workcaseid.

谢谢

推荐答案

更新:EF5 +支持多对多,而无需显式映射联接表.请参见此EF公告如对您的问题的评论所述,EF Core< = 2.2 问题,已在EF Core存储库的后锁中进行了跟踪,可能会使其成为3.0版..

UPDATE: EF5+ supports many-to-many without explicitly mapping the join table. See this EF announcement As commented on your question, EF Core <= 2.2 does not yet support many-to-many relationships without a join table. This is an issue tracked in the EF Core repo's backlock, and will maybe make it into version 3.0.

在您的情况下,您需要引入一个与两个父表都相关的新表.在这种情况下,您的模型会喜欢以下内容:

In your case you'll need to introduce a new table that relates to both parent tables. In this case, your model will like something like the following:

public class WorkCase
{
    public int WorkCaseId { get; set; }

    public int CaseDetailId { get; set; }

    public CaseDetail CaseDetail { get; set; }

    public ICollection<WorkCaseWorkflow> Workflows { get; set; }
}

public class Workflow
{
    public int WorkflowId { get; set; }

    public string Comment { get; set; }

    public DateTime? UpdateDate { get; set; }

    public ICollection<WorkCaseWorkflow> WorkCases { get; set; }
}

public class WorkCaseWorkflow
{
    public int WorkCaseId { get; set; }
    public WorkCase WorkCase { get; set; }

    public int WorkflowId { get; set; }
    public Workflow Workflow { get; set; }
}

然后在您的 DbContext 子类中,覆盖 OnModelCreating 并添加以下代码:

Then in your DbContext subclass, override the OnModelCreating and add the following code:

protected override void OnModelCreating(ModelBuilder builder)
{
    var ww = builder.Entity<WorkCaseWorkflow>();
    ww.HasKey(w => new { w.WorkCaseId, WorkflowId });
    ww.HasOne(w => w.WorkCase)
      .WithMany(wc => wc.Workflows)
      .HasForeignKey(w => w.WorkCaseId);
    ww.HasOne(w => w.Workflow)
      .WithMany(wc => wc.WorkCases)
      .HasForeignKey(w => w.WorkflowId);
}

我对您的模型不熟悉,但是您可以将共享属性移动到联接表中.

I'm not familiar with your model, but you can move the shared properties to the join table.

但是, @Arthur Vickers (EF Core开发的成员)中有大量文章团队)探讨如何简化多对多关系:

However, there is a great series of articles by @Arthur Vickers (a member in the EF Core dev team) on how to easen up many-to-many relationships:

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆