-
Notifications
You must be signed in to change notification settings - Fork 226
Settings.UseMappingTables
EF 6 only. Hides a pure many-to-many join table so EF maps the relationship implicitly, instead of generating an entity for it.
| Type | bool |
| Default | false |
| Applies to |
EF 6 only. Must stay false for EF Core |
| Databases | All |
In Database.tt? |
Yes |
A join table with nothing but two foreign keys - StudentCourse holding StudentId and CourseId - is
plumbing, not a concept. EF 6 can map it implicitly, giving Student.Courses and Course.Students and no
StudentCourse class at all:
modelBuilder.Entity<Student>()
.HasMany(s => s.Courses)
.WithMany(c => c.Students)
.Map(m => m.ToTable("StudentCourse").MapLeftKey("StudentId").MapRightKey("CourseId"));true does that. false generates StudentCourse as an ordinary entity with two one-to-many relationships,
and you navigate through it.
EF Core 5 and later support many-to-many without a join entity in your model, but this generator does not emit that mapping - it always generates the join entity for EF Core, which is a supported and explicit way to model it.
Setting this true with an EF Core template produces code that does not compile. The shipped Database.tt
leaves it false for that reason.
With EF Core, or with EF 6 and this off, the fixture's StudentCourse generates as an entity alongside the
others:
public interface IMyDbContext : IDisposable
public class MyDbContext : DbContext, IMyDbContext
public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
public class ActiveStudent
public class Course
public class Document
public class OrderLineItem
public class Student
public class StudentCourse
public class ActiveStudentConfiguration : IEntityTypeConfiguration<ActiveStudent>
public class CourseConfiguration : IEntityTypeConfiguration<Course>
public class DocumentConfiguration : IEntityTypeConfiguration<Document>
public class OrderLineItemConfiguration : IEntityTypeConfiguration<OrderLineItem>
public class StudentConfiguration : IEntityTypeConfiguration<Student>
public class StudentCourseConfiguration : IEntityTypeConfiguration<StudentCourse>
public class GetCourseReportReturnModel
public class ResultSetModel1
public class ResultSetModel2
public class GetStudentsByCourseReturnModel
public class sales_GetOrderTotalsReturnModelImplicit mapping reads better for a genuinely pure relationship. student.Courses is what you mean.
The explicit join entity survives change. The moment somebody adds EnrolledOn or Grade to the join
table, it stops being pure, EF 6 can no longer map it implicitly, and you have to reintroduce the entity and
rewrite every query that went through the collection. Join tables acquire columns more often than people
expect.
That is why EF Core dropped implicit mapping for a release and why the explicit entity is a defensible default even on EF 6.
A table only qualifies if it has nothing but the two foreign keys. Any third column, including an
identity primary key of its own, disqualifies it and it is generated as an entity whatever this is set to.
This is silent - you set true, nothing changes, and there is no message.
Turning it on removes the entity, so any code using it stops compiling. That is the point, and it is still a breaking change.
Naming the navigation properties is
Settings.MappingTableRename, which matters when two join
tables link the same pair - UserRequiredSkills and UserOptionalSkills would both otherwise produce
User.Skills.
- Settings.MappingTableRename - naming the collections
- Settings.DatabaseType and TemplateType - which template you are on
- Settings.ForeignKeyFilterFunc
- Settings Reference
- Settings A-Z - every setting, with a page each
- Common Settings Types Explained
- Settings Callbacks
- Settings runtime values and helpers
- Filtering
- Full Control Over the Generated Code
- Enum Generation from Table Data
- Owned Entities
- JSON column support
- Global Query Filters
- Extended Property Names Feature
- Partial Properties
- File-Scoped Namespaces
- Data Annotations
- Spatial Types
- HierarchyId
- RowVersion and TimeStamp columns
- Lazy Loading
- Stored proc result sets
- Custom File-Based Templates
- Extra entities via partial classes
- INotifyPropertyChanged
- Syntax colour for T4