Skip to content
Open
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 @@ -1204,7 +1204,9 @@ private void GenerateTableMapping(
}

if (schema != null
|| (schemaAnnotation != null && tableName != null))
|| (schemaAnnotation != null
&& tableName != null
&& entityType.GetDefaultSchema() != null))
{
stringBuilder
.Append(", ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,31 @@ public void Views_are_stored_in_the_model_snapshot()
"""),
o => Assert.Equal("EntityWithOneProperty", o.GetEntityTypes().Single().GetViewName()));

[Fact] // Issue #26067
public void ToTable_with_default_null_schema_is_not_changed_on_snapshot_round_trip()
{
// When the default schema is null, regenerating the snapshot from a model that was itself
// built from a snapshot (as happens when removing a migration) must not add a redundant
// ", (string)null" schema argument to every ToTable call.
var modelBuilder = CreateConventionalModelBuilder();
modelBuilder.HasDefaultSchema(null);
modelBuilder.Model.RemoveAnnotation(CoreAnnotationNames.ProductVersion);
modelBuilder.Entity<EntityWithOneProperty>().Ignore(e => e.EntityWithTwoProperties);

var model = modelBuilder.FinalizeModel(designTime: true, skipValidation: true);

var generator = CreateMigrationsGenerator();
var code = generator.GenerateSnapshot("RootNamespace", typeof(DbContext), "Snapshot", model);

Assert.Contains(@"b.ToTable(""EntityWithOneProperty"");", code);
Assert.DoesNotContain("(string)null", code);

var roundTrippedModel = BuildModelFromSnapshotSource(code);
var roundTrippedCode = generator.GenerateSnapshot("RootNamespace", typeof(DbContext), "Snapshot", roundTrippedModel);

Assert.Equal(code, roundTrippedCode, ignoreLineEndingDifferences: true);
}

[Fact]
public void Views_with_schemas_are_stored_in_the_model_snapshot()
=> Test(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,59 @@ public virtual Task Alter_column_reset_collation()
Assert.Null(nameColumn.Collation);
});

[Fact]
public virtual async Task Convert_owned_entity_with_no_schema_to_regular_entity()
=> await Test(
common =>
{
common.HasDefaultSchema(null);
common.Entity(
"Entity", e =>
{
e.Property<int>("Id").ValueGeneratedOnAdd();
e.HasKey("Id");
e.Property<string>("Name");
e.ToTable("Entity", "MySchema");
});
},
source =>
{
source.Entity(
"Entity", e =>
{
e.OwnsOne(
"Owned", "OwnedReference", o =>
{
o.Property<DateTime>("Date");
o.ToTable("Owned", (string)null);
});
});
},
target =>
{
target.Entity(
"Owned", e =>
{
e.Property<int>("EntityId").ValueGeneratedNever();
e.HasKey("EntityId");
e.Property<DateTime>("Date");
e.ToTable("Owned", (string)null);
});
},
model =>
{
Assert.Equal(2, model.Tables.Count());

var ownedTable = model.Tables.Single(t => t.Name == "Owned");
var entityTable = model.Tables.Single(t => t.Name == "Entity");

if (AssertSchemaNames)
{
Assert.Equal("MySchema", entityTable.Schema);
Assert.NotEqual("MySchema", ownedTable.Schema);
}
});

#pragma warning disable EF8001 // Owned JSON entities are obsolete
[Fact]
public virtual async Task Convert_json_entities_to_regular_owned()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,16 @@ FROM [sys].[columns] [c]
""");
}

public override async Task Convert_owned_entity_with_no_schema_to_regular_entity()
{
await base.Convert_owned_entity_with_no_schema_to_regular_entity();

AssertSql(
"""
ALTER TABLE [Owned] DROP CONSTRAINT [FK_Owned_Entity_EntityId];
""");
}
Comment thread
AndriySvyryd marked this conversation as resolved.

public override async Task Convert_json_entities_to_regular_owned()
{
await base.Convert_json_entities_to_regular_owned();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,41 @@ public override async Task Alter_column_reset_collation()
""");
}

public override async Task Convert_owned_entity_with_no_schema_to_regular_entity()
{
await base.Convert_owned_entity_with_no_schema_to_regular_entity();

AssertSql(
"""
CREATE TABLE "ef_temp_Owned" (
"EntityId" INTEGER NOT NULL CONSTRAINT "PK_Owned" PRIMARY KEY,
"Date" TEXT NOT NULL
);
""",
//
"""
INSERT INTO "ef_temp_Owned" ("EntityId", "Date")
SELECT "EntityId", "Date"
FROM "Owned";
""",
//
"""
PRAGMA foreign_keys = 0;
""",
//
"""
DROP TABLE "Owned";
""",
//
"""
ALTER TABLE "ef_temp_Owned" RENAME TO "Owned";
""",
//
"""
PRAGMA foreign_keys = 1;
""");
}

public override async Task Convert_json_entities_to_regular_owned()
{
await base.Convert_json_entities_to_regular_owned();
Expand Down
Loading