I have never been in the situation where I originally had two many-to-many's that were resolved. But then down another level is another many-to-many on both of those join tables. I am wondering if there is a better way to architect this w/ entity framework core. Here is what I got. I am trying to figure out which Project Owners are part of which Project Products.
I have 3 tables:
Project
Product
Owner
A Project can have many Products and a Project can have many Owners. A Product can have many Projects and an Owner can have many Projects. I resolved these two many-to-many relationships by doing the following:
ProjectProduct
Two keys: ProjectId, ProductId
ProjectOwner
Two keys: ProjectId, OwnerId
Additionally a ProjectProduct can have many ProjectOwners and a ProjectOwner can have many ProjectsProducts.
I thought the solution would be to add a unique Id to both ProjectOwner and ProjectProduct and create a new entity called ProjectProductOwner with the following keys:
ProjectProductId, ProjectOwnerId
Here is what my DBContext looks like:
// Key Specifications
modelBuilder.Entity<ProjectProductOwner>()
.HasKey(x => new { x.ProjectProductId, x.ProjectOwnerId });
// Project Product
modelBuilder.Entity<ProjectProduct>()
.HasOne(x => x.Project)
.WithMany(x => x.Products)
.HasForeignKey(x => x.ProjectId);
modelBuilder.Entity<ProjectProduct>()
.HasOne(x => x.Product)
.WithMany(x => x.Projects)
.HasForeignKey(x => x.ProductId);
// Project Owner
modelBuilder.Entity<ProjectOwner>()
.HasOne(x => x.Project)
.WithMany(x => x.Owners)
.HasForeignKey(x => x.ProjectId);
modelBuilder.Entity<ProjectOwner>()
.HasOne(x => x.Owner)
.WithMany(x => Projects)
.HasForeignKey(x => x.OwnerId);
// Project Product Owner
modelBuilder.Entity<ProjectProductOwner>()
.HasOne(x => x.ProjectProduct)
.WithMany(x => x.ProjectOwners)
.HasForeignKey(x => x.ProjectProductId);
modelBuilder.Entity<ProjectProductOwner>()
.HasOne(x => x.ProjectOwner)
.WithMany(x => x.ProjectProducts)
.HasForeignKey(x => x.ProjectOwnerId);
I am getting the error: Introducing FOREIGN KEY constraint 'FK_ProjectProductOwner_ProjectProducts_ProjectProductId' on table 'ProjectProductOwner' may cause cycles or multiple cascade paths.
additionally:
'FK_ProjectProductOwner_ProjectOwners_ProjectOwnerId' on table 'ProjectProductOwner' may cause cycles or multiple cascade paths.
Owners
to the project, so you can only add products that are "owned" by the already added owners of the project? Also: Is this a new database or something that already exists?