Description
I have a simple many to many relation defined between the Course
and Student
entities
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public List<Course> Courses { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string Name { get; set; }
public List<Student> Students { get; set; }
}
I configured both entities to use my_schema
database schema
modelBuilder.Entity<Student>()
.ToTable(nameof(Student), schema: "my_schema");
modelBuilder.Entity<Course>()
.ToTable(nameof(Course), schema: "my_schema");
After running the generated migration, database tables are generated as expected
Autogenerated join table has the same schema as tables it is related to. And this is causing the poblems.
Each new migration will now include strange (and useless) RenameTable
operation:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameTable(
name: "CourseStudent",
newName: "CourseStudent",
newSchema: "my_schema");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameTable(
name: "CourseStudent",
schema: "my_schema",
newName: "CourseStudent");
}
I am using EntityFramework Core version 5.0.2.
I see in generated Snapshot migration file that CourseStudent
entity maps to table without specifying the schema
modelBuilder.Entity("CourseStudent", b =>
{
b.Property<int>("CoursesCourseId")
.HasColumnType("int");
b.Property<int>("StudentsStudentId")
.HasColumnType("int");
b.HasKey("CoursesCourseId", "StudentsStudentId");
b.HasIndex("StudentsStudentId");
b.ToTable("CourseStudent");
});
With this information missing, I suppose, the tool generating new migrations is stuck in a loop where it is changing the schema for join table, but forgetting to note this somewhere (snapshot).