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

Creating two tables with same name and different schema messes up the migrations if tables have FKs between them #17773

Closed
javiercampos opened this issue Sep 11, 2019 · 1 comment

Comments

@javiercampos
Copy link

javiercampos commented Sep 11, 2019

Sorry for the long title. Basically I'm having a problem when two tables have the same name but different schema (using Microsoft.EntityFrameworkCore.SqlServer) when those tables have a relationship between them.

Say this extremely simple case:

class Foo
{
	public Guid Id { get; set; }
	public string OtherProperty { get; set; }
	public int OnePropertyCompletelyUnrelatedToTheBarClass { get; set; }
}

class Bar
{
	public Guid Id { get; set; }
}

This works fine with this model configuration:

protected override void OnModelCreating(ModelBuilder b)
{
	b.Entity<Foo>(opt =>
	{
		opt.ToTable("Foo", "SchemaForFoo");
		opt.HasKey(x => x.Id);
		opt.Property(x => x.Id).ValueGeneratedNever();
		opt.HasOne<Bar>().WithOne().HasForeignKey<Foo>(x => x.Id);
	});
	b.Entity<Bar>(opt =>
	{
		opt.ToTable("Bar", "SchemaForBar");
		opt.HasKey(x => x.Id);
	});
}

The migration is correct and I get a 1:1 relationship FK constraint so that both Foo and Bar have to use the same Id. This is correct and what I want.

However if I change this:

	b.Entity<Bar>(opt =>
	{
		opt.ToTable("Bar", "SchemaForBar");

to this, so both tables for Foo and Bar, have the same name, but different schema:

	b.Entity<Bar>(opt =>
	{
		opt.ToTable("Foo", "SchemaForBar");

And I add a migration, I end up with this:

protected override void Up(MigrationBuilder migrationBuilder)
{
	migrationBuilder.EnsureSchema(
		name: "SchemaForBar");

	migrationBuilder.EnsureSchema(
		name: "SchemaForFoo");

	migrationBuilder.CreateTable(
		name: "Foo",
		schema: "SchemaForBar",
		columns: table => new
		{
			Id = table.Column<Guid>(nullable: false),
			OtherProperty = table.Column<string>(nullable: true),
			OnePropertyCompletelyUnrelatedToTheBarClass = table.Column<int>(nullable: false)
		},
		constraints: table =>
		{
			table.PrimaryKey("PK_Baz", x => x.Id);
		});

	migrationBuilder.CreateTable(
		name: "Foo",
		schema: "SchemaForFoo",
		columns: table => new
		{
			Id = table.Column<Guid>(nullable: false),
			OtherProperty = table.Column<string>(nullable: true),
			OnePropertyCompletelyUnrelatedToTheBarClass = table.Column<int>(nullable: false)
		},
		constraints: table =>
		{
			table.PrimaryKey("PK_Baz", x => x.Id);
			table.ForeignKey(
				name: "FK_Baz_Baz_Id",
				column: x => x.Id,
				principalSchema: "SchemaForBar",
				principalTable: "Baz",
				principalColumn: "Id",
				onDelete: ReferentialAction.Cascade);
		});
}

As you can see, for the Bar entity (which now points to the table SchemaForBar.Foo), it added all the columns for the properties in Foo (including two completely unrelated string and int properties).

Again, this only happens if both tables are named the same (although in a different schema) -and- if they have a one-to-one relationship (I haven't tested with other relationships)

I've just tried this with EF Core 3.0.0-preview9.19423.6 (latest available on general nuget)

The workaround is easy enough: just don't call the tables the same, however I believe this should be looked upon, because other bugs may arise in this case: looks to me that "something" at some point on the migration builder is not taking the schema into account for the "uniqueness" of the Entity -> Table conversion and just checking a name

@bricelam
Copy link
Contributor

bricelam commented Oct 3, 2019

Duplicate of #14027

@bricelam bricelam marked this as a duplicate of #14027 Oct 3, 2019
@bricelam bricelam closed this as completed Oct 3, 2019
@bricelam bricelam removed this from the 3.1.0 milestone Oct 3, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants