Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entityframework core how to map Many-to-Many without creating a join table

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

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.

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; }
    }

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.

enter image description here

Thanks

like image 333
Mukil Deepthi Avatar asked Jan 28 '23 21:01

Mukil Deepthi


1 Answers

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; }
}

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.


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:

  • Part 1: The basics
  • Part 2: Hiding as IEnumerable
  • Part 3: Hiding as ICollection
  • Part 4: A more general abstraction
like image 157
Shimmy Weitzhandler Avatar answered Apr 07 '23 16:04

Shimmy Weitzhandler