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

Migrations: Only create FKs once in a hierarchy #2910

Merged
merged 1 commit into from
Aug 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ protected virtual IEnumerable<MigrationOperation> Diff(
Diff(Annotations.For(source).Sequences, Annotations.For(target).Sequences))
.Concat(
Diff(
source.EntityTypes.SelectMany(t => t.GetForeignKeys()),
target.EntityTypes.SelectMany(t => t.GetForeignKeys()),
source.EntityTypes.SelectMany(t => t.GetDeclaredForeignKeys()),
target.EntityTypes.SelectMany(t => t.GetDeclaredForeignKeys()),
diffContext))
: target != null
? Add(target, diffContext)
Expand All @@ -243,7 +243,7 @@ protected virtual IEnumerable<MigrationOperation> Add(IModel target, DiffContext
=> GetSchemas(target).SelectMany(Add)
.Concat(target.GetRootEntityTypes().SelectMany(t => Add(t, diffContext)))
.Concat(Annotations.For(target).Sequences.SelectMany(Add))
.Concat(target.EntityTypes.SelectMany(t => t.GetForeignKeys()).SelectMany(k => Add(k, diffContext)));
.Concat(target.EntityTypes.SelectMany(t => t.GetDeclaredForeignKeys()).SelectMany(k => Add(k, diffContext)));

protected virtual IEnumerable<MigrationOperation> Remove(IModel source, DiffContext diffContext) =>
source.GetRootEntityTypes().SelectMany(t => Remove(t, diffContext))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3018,6 +3018,48 @@ public void Drop_index_on_subtype()
});
}

[Fact]
public void Create_table_with_foreign_key_on_base_type()
{
Execute(
_ => { },
modelBuilder =>
{
modelBuilder.Entity(
"Person",
x =>
{
x.Property<int>("Id");
x.Key("Id");
});
modelBuilder.Entity(
"Animal",
x =>
{
x.Property<int>("Id");
x.Key("Id");
x.Property<int>("HandlerId");
x.Reference("Person").InverseCollection().ForeignKey("HandlerId");
});
modelBuilder.Entity("Wyvern").BaseType("Animal");
},
operations =>
{
Assert.Equal(2, operations.Count);
Assert.IsType<CreateTableOperation>(operations[0]);

var createTableOperation = Assert.IsType<CreateTableOperation>(operations[1]);
Assert.Equal("Animal", createTableOperation.Name);
Assert.Equal(1, createTableOperation.ForeignKeys.Count);

var addForeignKeyOperation = createTableOperation.ForeignKeys[0];
Assert.Equal("FK_Animal_Person_HandlerId", addForeignKeyOperation.Name);
Assert.Equal(new[] { "HandlerId" }, addForeignKeyOperation.Columns);
Assert.Equal("Person", addForeignKeyOperation.PrincipalTable);
Assert.Equal(new[] { "Id" }, addForeignKeyOperation.PrincipalColumns);
});
}

[Fact]
public void Create_table_with_foreign_key_on_subtype()
{
Expand Down Expand Up @@ -3173,6 +3215,62 @@ public void Create_table_with_selfReferencing_foreign_key_in_hierarchy()
});
}

[Fact]
public void Add_foreign_key_on_base_type()
{
Execute(
modelBuilder =>
{
modelBuilder.Entity(
"Person",
x =>
{
x.Property<int>("Id");
x.Key("Id");
});
modelBuilder.Entity(
"Animal",
x =>
{
x.Property<int>("Id");
x.Key("Id");
x.Property<int>("HandlerId");
});
modelBuilder.Entity("Drakee").BaseType("Animal");
},
modelBuilder =>
{
modelBuilder.Entity(
"Person",
x =>
{
x.Property<int>("Id");
x.Key("Id");
});
modelBuilder.Entity(
"Animal",
x =>
{
x.Property<int>("Id");
x.Key("Id");
x.Property<int>("HandlerId");
x.Reference("Person").InverseCollection().ForeignKey("HandlerId");
});
modelBuilder.Entity("Drakee").BaseType("Animal");
},
operations =>
{
Assert.Equal(1, operations.Count);

var operation = Assert.IsType<AddForeignKeyOperation>(operations[0]);
Assert.Equal("Animal", operation.Table);
Assert.Equal("FK_Animal_Person_HandlerId", operation.Name);
Assert.Equal(new[] { "HandlerId" }, operation.Columns);
Assert.Equal("Person", operation.PrincipalTable);
Assert.Equal(new[] { "Id" }, operation.PrincipalColumns);
});
}

[Fact]
public void Add_foreign_key_on_subtype()
{
Expand Down