Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration loop in m:n relation with custom schema #23937

Closed
tomaschabada opened this issue Jan 22, 2021 · 4 comments · Fixed by #26095
Closed

Migration loop in m:n relation with custom schema #23937

tomaschabada opened this issue Jan 22, 2021 · 4 comments · Fixed by #26095
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@tomaschabada
Copy link

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
database diagram

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).

@ajcvickers
Copy link
Member

/cc @AndriySvyryd @bricelam We fixed a bug where the join table was not in the same schema as the two other tables, but it looks like the snapshot might not have picked this up.

@tomaschabada
Copy link
Author

/cc @AndriySvyryd @bricelam We fixed a bug where the join table was not in the same schema as the two other tables, but it looks like the snapshot might not have picked this up.

But the problem occurs when the join table is in the same schema as tables that it joins

@AndriySvyryd
Copy link
Member

@tomaschabada The workaround is to specify the schema explicitly:

modelBuilder
    .Entity<Student>()
    .HasMany(l => l.Courses)
    .WithMany(r => r.Students)
    .UsingEntity(a => a.ToTable("CourseStudent", "my_schema"));

@tomaschabada
Copy link
Author

@tomaschabada The workaround is to specify the schema explicitly:

modelBuilder
    .Entity<Student>()
    .HasMany(l => l.Courses)
    .WithMany(r => r.Students)
    .UsingEntity(a => a.ToTable("CourseStudent", "my_schema"));

I know there are workarounds. We will use one of them till the issue will be fixed

@ajcvickers ajcvickers added this to the 6.0.0 milestone Jan 26, 2021
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 17, 2021
@AndriySvyryd AndriySvyryd removed their assignment Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants