Closed bgoodearl closed 1 year ago
I didn't get any exceptions after uncommenting the collections in Department
and DepartmentFacilityType
. Perhaps this is related to the manual changes you made.
To configure the FK properties on the join type you could do something like this:
e.HasMany(e => e.CoursePresentationTypes).WithMany(p => p.Courses)
.UsingEntity(
join => join
.HasOne(typeof(CoursePresentationType))
.WithMany()
.HasForeignKey("CoursePresentationTypesLookupTypeId", "CoursePresentationTypesCode"),
join => join
.HasOne(typeof(Course))
.WithMany()
.HasForeignKey("CoursesCourseId"),
join =>
{
join.ToTable("_coursesPresentationTypes")
.Property<int>("CoursesCourseId").HasColumnName("CourseID");
join.Property<short>("CoursePresentationTypesLookupTypeId").HasColumnName("LookupTypeId");
join.Property<string>("CoursePresentationTypesCode").HasColumnName("CoursePresentationTypeCode");
}
);
Using an explicit type for the join entity type would make writing this code easier.
Context
I have a large client solution that started life as ASP.NET MVC with .NET Framework 4.5 and EF 6.1 back in 2014. It has a large number of many-to-many relationships, and a fair percentage of those have a composite key on one side. The production database has more than 100 tables in it.
We've migrated that code to .NET Core 3.1 with EF 6.4, and are now in the process of figuring out how to rebuild the app in .NET 6 with EF Core 6 so that it can operate on the same database as the older code, and we're running into problems with the many-to-many relationships.
In the early part of this year, I created two solutions based on the models in the Contoso University code to look at multiple issues and possibilities, including migrating from a layered architecture toward a Clean architecture. The results of that work can be found in these two repositories: ContosoU_dnc31_MVCB_Clean (written with .NET Core 3.1 and EF 6.x) and ContosoU_dn6_MVCB_Clean (written with .NET 6 and EF Core 6).
The Problem - specifying Many-to-Many relationships to match the existing object model and schema
I added code to the "dnc31" version that has a couple of "lookup" models.
Here are some of the relevant classes from the "with_lookups" branch of ContosoU_dnc31_MVCB_Clean
and relevant code from OnModelCreating:
I then spent a considerable amount of time trying to figure out how to do the same thing with EF Core 6, and was unable to find anything in the documentation (such as it is) or online samples, blog posts, etc. to help me out. I reached out to a well-known expert in the field, and with much online searching, guesses, and trial-and-error, and more than 2 hours on zoom, we got as far as the following (which can be found in the "with_lookups" branch of ContosoU_dn6_MVCB_Clean):
With the collections commented out in Department and DepartmentFacilityType, we were able to get a many-to-many defined for Course and CoursePresentationType, and figure out how to rename the simple key for CourseID. This afternoon, I was able to run the migration and then tweak the generated code in the snapshot and migration to get it to generated the schema I wanted, but when I uncommented the collections in Department and DepartmentFacilityType, the next migration I ran blew up.
Is there a way to specify the database column names for the composite key in the join table?
Include provider and version information
EF Core version: 6.0.2 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 6.0 Operating system: Windows 10 IDE: Visual Studio 2022 17.0.10