From 0c5cec34728f08098d9a650c793ad3384671ffdb Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Sun, 5 Feb 2023 15:19:40 +0000 Subject: [PATCH 1/2] Use default schema for TPC tables Fixes #29899 --- .../RelationalEntityTypeExtensions.cs | 2 +- .../Migrations/ModelSnapshotSqlServerTest.cs | 413 ++++++++++-------- 2 files changed, 220 insertions(+), 195 deletions(-) diff --git a/src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs b/src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs index c2606990b1d..7d859cba190 100644 --- a/src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs +++ b/src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs @@ -146,7 +146,7 @@ public static void SetTableName(this IMutableEntityType entityType, string? name } return entityType.BaseType != null - ? entityType.GetRootType().GetSchema() + ? entityType.GetRootType().GetSchema() ?? GetDefaultSchema(entityType) : GetDefaultSchema(entityType); } diff --git a/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs b/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs index a05d59d8942..3885e213a17 100644 --- a/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs +++ b/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs @@ -331,6 +331,7 @@ public virtual void Model_annotations_are_stored_in_snapshot() AddBoilerPlate( """ modelBuilder + .HasDefaultSchema("DefaultSchema") .HasAnnotation("AnnotationName", "AnnotationValue") .HasAnnotation("Relational:MaxIdentifierLength", 128); @@ -341,7 +342,7 @@ public virtual void Model_annotations_are_stored_in_snapshot() """), o => { - Assert.Equal(8, o.GetAnnotations().Count()); + Assert.Equal(9, o.GetAnnotations().Count()); Assert.Equal("AnnotationValue", o["AnnotationName"]); }); @@ -356,7 +357,9 @@ public virtual void Model_Fluent_APIs_are_properly_generated() }, AddBoilerPlate( """ - modelBuilder.HasAnnotation("Relational:MaxIdentifierLength", 128); + modelBuilder + .HasDefaultSchema("DefaultSchema") + .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseHiLo(modelBuilder, "EntityFrameworkHiLoSequence"); @@ -373,7 +376,7 @@ public virtual void Model_Fluent_APIs_are_properly_generated() b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); """), o => @@ -395,7 +398,9 @@ public virtual void Model_fluent_APIs_for_sequence_key_are_properly_generated() }, AddBoilerPlate( """ - modelBuilder.HasAnnotation("Relational:MaxIdentifierLength", 128); + modelBuilder + .HasDefaultSchema("DefaultSchema") + .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseKeySequences(modelBuilder, "Sequence"); @@ -406,13 +411,13 @@ public virtual void Model_fluent_APIs_for_sequence_key_are_properly_generated() b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("int") - .HasDefaultValueSql("NEXT VALUE FOR [EntityWithOnePropertySequence]"); + .HasDefaultValueSql("NEXT VALUE FOR [DefaultSchema].[EntityWithOnePropertySequence]"); SqlServerPropertyBuilderExtensions.UseSequence(b.Property("Id")); b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); """), o => @@ -468,7 +473,7 @@ public virtual void Entities_are_stored_in_model_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -484,7 +489,7 @@ public virtual void Entities_are_stored_in_model_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => @@ -521,7 +526,7 @@ public virtual void Entities_are_stored_in_model_snapshot_for_TPT() b.HasKey("Id"); - b.ToTable("AbstractBase"); + b.ToTable("AbstractBase", "DefaultSchema"); b.UseTptMappingStrategy(); }); @@ -530,7 +535,7 @@ public virtual void Entities_are_stored_in_model_snapshot_for_TPT() { b.HasBaseType("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+AbstractBase"); - b.ToTable("BaseEntity"); + b.ToTable("BaseEntity", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+DerivedEntity", b => @@ -554,7 +559,7 @@ public virtual void Entities_are_stored_in_model_snapshot_for_TPT() """), model => { - Assert.Equal(4, model.GetAnnotations().Count()); + Assert.Equal(5, model.GetAnnotations().Count()); Assert.Equal(3, model.GetEntityTypes().Count()); var abstractBase = model.FindEntityType("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+AbstractBase"); @@ -563,6 +568,7 @@ public virtual void Entities_are_stored_in_model_snapshot_for_TPT() var baseType = model.FindEntityType("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+BaseEntity"); Assert.Equal("BaseEntity", baseType.GetTableName()); + Assert.Equal("DefaultSchema", baseType.GetSchema()); var derived = model.FindEntityType("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+DerivedEntity"); Assert.Equal("DerivedEntity", derived.GetTableName()); @@ -595,7 +601,7 @@ public virtual void Entities_are_stored_in_model_snapshot_for_TPT_with_one_exclu b.HasKey("Id"); - b.ToTable("BaseEntity"); + b.ToTable("BaseEntity", "DefaultSchema"); b.UseTptMappingStrategy(); }); @@ -624,7 +630,7 @@ public virtual void Entities_are_stored_in_model_snapshot_for_TPT_with_one_exclu """), o => { - Assert.Equal(4, o.GetAnnotations().Count()); + Assert.Equal(5, o.GetAnnotations().Count()); Assert.Equal( "DerivedEntity", @@ -648,7 +654,7 @@ public void Views_are_stored_in_the_model_snapshot() b.ToTable((string)null); - b.ToView("EntityWithOneProperty", (string)null); + b.ToView("EntityWithOneProperty", "DefaultSchema"); }); """), o => Assert.Equal("EntityWithOneProperty", o.GetEntityTypes().Single().GetViewName())); @@ -701,7 +707,7 @@ public virtual void Entities_are_stored_in_model_snapshot_for_TPC() b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("int") - .HasDefaultValueSql("NEXT VALUE FOR [AbstractBaseSequence]"); + .HasDefaultValueSql("NEXT VALUE FOR [DefaultSchema].[AbstractBaseSequence]"); SqlServerPropertyBuilderExtensions.UseSequence(b.Property("Id")); @@ -716,7 +722,7 @@ public virtual void Entities_are_stored_in_model_snapshot_for_TPC() { b.HasBaseType("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+AbstractBase"); - b.ToTable("BaseEntity"); + b.ToTable("BaseEntity", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+DerivedEntity", b => @@ -733,7 +739,7 @@ public virtual void Entities_are_stored_in_model_snapshot_for_TPC() """), model => { - Assert.Equal(5, model.GetAnnotations().Count()); + Assert.Equal(6, model.GetAnnotations().Count()); Assert.Equal(3, model.GetEntityTypes().Count()); var abstractBase = model.FindEntityType("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+AbstractBase"); @@ -762,13 +768,13 @@ public virtual void Entity_splitting_is_stored_in_snapshot_with_tables() b.Property("Shadow").HasColumnName("Shadow"); b.ToTable( - "Order", tb => + "Order", "DefaultSchema", tb => { tb.Property(e => e.Id).UseIdentityColumn(2, 3).HasAnnotation("fii", "arr"); tb.Property("Shadow"); }); b.SplitToTable( - "SplitOrder", sb => + "SplitOrder", "DefaultSchema", sb => { sb.Property("Shadow"); sb.HasTrigger("splitTrigger").HasAnnotation("oof", "rab"); @@ -782,12 +788,12 @@ public virtual void Entity_splitting_is_stored_in_snapshot_with_tables() od.Property("BillingShadow"); od.ToTable( - "SplitOrder", tb => + "SplitOrder", "DefaultSchema", tb => { tb.Property("BillingShadow").HasColumnName("Shadow"); }); od.SplitToTable( - "BillingDetails", sb => + "BillingDetails", "DefaultSchema", sb => { sb.Property("BillingShadow").HasColumnName("Shadow"); }); @@ -800,12 +806,12 @@ public virtual void Entity_splitting_is_stored_in_snapshot_with_tables() od.Property("ShippingShadow"); od.ToTable( - "Order", tb => + "Order", "DefaultSchema", tb => { tb.Property("ShippingShadow").HasColumnName("Shadow"); }); od.SplitToTable( - "ShippingDetails", sb => + "ShippingDetails", "DefaultSchema", sb => { sb.Property("ShippingShadow"); }); @@ -829,7 +835,7 @@ public virtual void Entity_splitting_is_stored_in_snapshot_with_tables() b.HasKey("Id"); - b.ToTable("Order", null, t => + b.ToTable("Order", "DefaultSchema", t => { t.Property("Id") .HasAnnotation("fii", "arr") @@ -840,7 +846,7 @@ public virtual void Entity_splitting_is_stored_in_snapshot_with_tables() t.Property("Shadow"); }); - b.SplitToTable("SplitOrder", null, t => + b.SplitToTable("SplitOrder", "DefaultSchema", t => { t.HasTrigger("splitTrigger") .HasAnnotation("oof", "rab"); @@ -871,13 +877,13 @@ public virtual void Entity_splitting_is_stored_in_snapshot_with_tables() b1.HasKey("OrderId"); - b1.ToTable("SplitOrder", null, t => + b1.ToTable("SplitOrder", "DefaultSchema", t => { t.Property("BillingShadow") .HasColumnName("Shadow"); }); - b1.SplitToTable("BillingDetails", null, t => + b1.SplitToTable("BillingDetails", "DefaultSchema", t => { t.Property("BillingShadow") .HasColumnName("Shadow"); @@ -902,7 +908,7 @@ public virtual void Entity_splitting_is_stored_in_snapshot_with_tables() b2.HasKey("OrderDetailsOrderId"); - b2.ToTable("SplitOrder"); + b2.ToTable("SplitOrder", "DefaultSchema"); b2.WithOwner() .HasForeignKey("OrderDetailsOrderId"); @@ -921,13 +927,13 @@ public virtual void Entity_splitting_is_stored_in_snapshot_with_tables() b1.HasKey("OrderId"); - b1.ToTable("Order", null, t => + b1.ToTable("Order", "DefaultSchema", t => { t.Property("ShippingShadow") .HasColumnName("Shadow"); }); - b1.SplitToTable("ShippingDetails", null, t => + b1.SplitToTable("ShippingDetails", "DefaultSchema", t => { t.Property("ShippingShadow"); }); @@ -951,7 +957,7 @@ public virtual void Entity_splitting_is_stored_in_snapshot_with_tables() b2.HasKey("OrderDetailsOrderId"); - b2.ToTable("ShippingDetails", (string)null); + b2.ToTable("ShippingDetails", "DefaultSchema"); b2.WithOwner() .HasForeignKey("OrderDetailsOrderId"); @@ -1095,12 +1101,12 @@ public virtual void Entity_splitting_is_stored_in_snapshot_with_views() b.ToTable((string)null); - b.ToView("EntityWithOneProperty", null, v => + b.ToView("EntityWithOneProperty", "DefaultSchema", v => { v.Property("Shadow"); }); - b.SplitToView("SplitView", null, v => + b.SplitToView("SplitView", "DefaultSchema", v => { v.Property("Shadow"); }); @@ -1120,13 +1126,13 @@ public virtual void Entity_splitting_is_stored_in_snapshot_with_views() b1.ToTable((string)null); - b1.ToView("EntityWithOneProperty", null, v => + b1.ToView("EntityWithOneProperty", "DefaultSchema", v => { v.Property("AlternateId") .HasColumnName("SomeId"); }); - b1.SplitToView("SplitView", null, v => + b1.SplitToView("SplitView", "DefaultSchema", v => { v.Property("AlternateId") .HasColumnName("SomeOtherId"); @@ -1156,7 +1162,7 @@ public virtual void Entity_splitting_is_stored_in_snapshot_with_views() Assert.Empty(relationalModel.Tables); Assert.Equal(2, relationalModel.Views.Count()); - var mainView = relationalModel.FindView(entityWithOneProperty.GetViewName(), entityWithOneProperty.GetSchema()); + var mainView = relationalModel.FindView(entityWithOneProperty.GetViewName(), "DefaultSchema"); var fragment = entityWithOneProperty.GetMappingFragments().Single(); var splitView = relationalModel.FindView(fragment.StoreObject.Name, fragment.StoreObject.Schema); @@ -1308,7 +1314,7 @@ public virtual void Sequence_is_stored_in_snapshot_as_fluent_api() """), model => { - Assert.Equal(5, model.GetAnnotations().Count()); + Assert.Equal(6, model.GetAnnotations().Count()); var sequence = model.GetSequences().Single(); Assert.Equal(2, sequence.StartValue); @@ -1385,7 +1391,7 @@ public virtual void CheckConstraint_is_stored_in_snapshot_as_fluent_api() b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties", t => + b.ToTable("EntityWithTwoProperties", "DefaultSchema", t => { t.HasCheckConstraint("AlternateId", "AlternateId > Id") .HasName("CK_Customer_AlternateId") @@ -1427,7 +1433,7 @@ public virtual void CheckConstraint_is_only_stored_in_snapshot_once_for_TPH() b.HasKey("Id"); - b.ToTable("BaseEntity"); + b.ToTable("BaseEntity", "DefaultSchema"); b.HasDiscriminator("Discriminator").HasValue("BaseEntity"); @@ -1477,7 +1483,7 @@ public virtual void Trigger_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithOneProperty", t => + b.ToTable("EntityWithOneProperty", "DefaultSchema", t => { t.HasTrigger("SomeTrigger") .HasDatabaseName("SomeTrg") @@ -1522,7 +1528,7 @@ public virtual void Triggers_and_ExcludeFromMigrations_are_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithOneProperty", t => + b.ToTable("EntityWithOneProperty", "DefaultSchema", t => { t.ExcludeFromMigrations(); @@ -1564,13 +1570,15 @@ public virtual void Model_use_identity_columns() builder => builder.UseIdentityColumns(), AddBoilerPlate( """ - modelBuilder.HasAnnotation("Relational:MaxIdentifierLength", 128); + modelBuilder + .HasDefaultSchema("DefaultSchema") + .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); """), o => { - Assert.Equal(4, o.GetAnnotations().Count()); + Assert.Equal(5, o.GetAnnotations().Count()); Assert.Equal(SqlServerValueGenerationStrategy.IdentityColumn, o.GetValueGenerationStrategy()); Assert.Equal(1, o.GetIdentitySeed()); Assert.Equal(1, o.GetIdentityIncrement()); @@ -1582,13 +1590,15 @@ public virtual void Model_use_identity_columns_custom_seed() builder => builder.UseIdentityColumns(5), AddBoilerPlate( """ - modelBuilder.HasAnnotation("Relational:MaxIdentifierLength", 128); + modelBuilder + .HasDefaultSchema("DefaultSchema") + .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 5L); """), o => { - Assert.Equal(4, o.GetAnnotations().Count()); + Assert.Equal(5, o.GetAnnotations().Count()); Assert.Equal(SqlServerValueGenerationStrategy.IdentityColumn, o.GetValueGenerationStrategy()); Assert.Equal(5, o.GetIdentitySeed()); Assert.Equal(1, o.GetIdentityIncrement()); @@ -1600,13 +1610,15 @@ public virtual void Model_use_identity_columns_custom_increment() builder => builder.UseIdentityColumns(increment: 5), AddBoilerPlate( """ - modelBuilder.HasAnnotation("Relational:MaxIdentifierLength", 128); + modelBuilder + .HasDefaultSchema("DefaultSchema") + .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 5); """), o => { - Assert.Equal(4, o.GetAnnotations().Count()); + Assert.Equal(5, o.GetAnnotations().Count()); Assert.Equal(SqlServerValueGenerationStrategy.IdentityColumn, o.GetValueGenerationStrategy()); Assert.Equal(1, o.GetIdentitySeed()); Assert.Equal(5, o.GetIdentityIncrement()); @@ -1625,12 +1637,14 @@ public virtual void Model_use_identity_columns_custom_seed_increment() b.HasKey("Id"); - b.ToTable("Buildings"); + b.ToTable("Buildings", "DefaultSchema"); }); }, AddBoilerPlate( """ - modelBuilder.HasAnnotation("Relational:MaxIdentifierLength", 128); + modelBuilder + .HasDefaultSchema("DefaultSchema") + .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 9223372036854775807L, 5); @@ -1644,12 +1658,12 @@ public virtual void Model_use_identity_columns_custom_seed_increment() b.HasKey("Id"); - b.ToTable("Buildings", (string)null); + b.ToTable("Buildings", "DefaultSchema"); }); """), o => { - Assert.Equal(4, o.GetAnnotations().Count()); + Assert.Equal(5, o.GetAnnotations().Count()); Assert.Equal(SqlServerValueGenerationStrategy.IdentityColumn, o.GetValueGenerationStrategy()); Assert.Equal(long.MaxValue, o.GetIdentitySeed()); Assert.Equal(5, o.GetIdentityIncrement()); @@ -1685,7 +1699,7 @@ public virtual void EntityType_annotations_are_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); b.HasAnnotation("AnnotationName", "AnnotationValue"); }); @@ -1719,7 +1733,7 @@ public virtual void EntityType_Fluent_APIs_are_properly_generated() SqlServerKeyBuilderExtensions.IsClustered(b.HasKey("Id"), false); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); SqlServerEntityTypeBuilderExtensions.IsMemoryOptimized(b); }); @@ -1752,7 +1766,7 @@ public virtual void BaseType_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("BaseEntity"); + b.ToTable("BaseEntity", "DefaultSchema"); b.HasDiscriminator("Discriminator").HasValue("BaseEntity"); @@ -1823,7 +1837,7 @@ public virtual void Discriminator_annotations_are_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("BaseEntity"); + b.ToTable("BaseEntity", "DefaultSchema"); b.HasDiscriminator("Discriminator").IsComplete(true).HasValue("BaseEntity"); @@ -1899,7 +1913,7 @@ public virtual void Converted_discriminator_annotations_are_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("BaseEntityWithStructDiscriminator"); + b.ToTable("BaseEntityWithStructDiscriminator", "DefaultSchema"); b.HasDiscriminator("Discriminator").IsComplete(true).HasValue("Base"); @@ -1969,7 +1983,7 @@ public virtual void Properties_are_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => @@ -2004,7 +2018,7 @@ public virtual void Primary_key_is_stored_in_snapshot() b.HasKey("Id", "AlternateId"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => @@ -2029,7 +2043,7 @@ public void HasNoKey_is_handled() b.Property("Id") .HasColumnType("int"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); """), o => @@ -2067,7 +2081,7 @@ public virtual void Alternate_keys_are_stored_in_snapshot() b.HasAlternateKey("Id", "AlternateId"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => @@ -2105,7 +2119,7 @@ public virtual void Indexes_are_stored_in_snapshot() b.HasIndex("AlternateId"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => @@ -2141,7 +2155,7 @@ public virtual void Indexes_are_stored_in_snapshot_including_composite_index() b.HasIndex("Id", "AlternateId"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => @@ -2177,7 +2191,7 @@ public virtual void Foreign_keys_are_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -2196,7 +2210,7 @@ public virtual void Foreign_keys_are_stored_in_snapshot() b.HasIndex("AlternateId") .IsUnique(); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -2398,7 +2412,7 @@ public virtual void Can_override_table_name_for_many_to_many_join_table_stored_i b.HasIndex("RightsId"); - b.ToTable("MyJoinTable", (string)null); + b.ToTable("MyJoinTable", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+ManyToManyLeft", b => @@ -2414,7 +2428,7 @@ public virtual void Can_override_table_name_for_many_to_many_join_table_stored_i b.HasKey("Id"); - b.ToTable("ManyToManyLeft"); + b.ToTable("ManyToManyLeft", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+ManyToManyRight", b => @@ -2430,7 +2444,7 @@ public virtual void Can_override_table_name_for_many_to_many_join_table_stored_i b.HasKey("Id"); - b.ToTable("ManyToManyRight"); + b.ToTable("ManyToManyRight", "DefaultSchema"); }); modelBuilder.Entity("ManyToManyLeftManyToManyRight", b => @@ -2540,7 +2554,7 @@ public virtual void TableName_preserved_when_generic() b.HasKey("Id"); - b.ToTable("EntityWithGenericKey"); + b.ToTable("EntityWithGenericKey", "DefaultSchema"); }); """, usingSystem: true), model => @@ -2591,7 +2605,7 @@ public virtual void Shared_columns_are_stored_in_the_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithProperties", (string)null); + b.ToTable("EntityWithProperties", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -2606,7 +2620,7 @@ public virtual void Shared_columns_are_stored_in_the_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithProperties", (string)null); + b.ToTable("EntityWithProperties", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -2655,7 +2669,7 @@ public virtual void PrimaryKey_name_preserved_when_generic() b.HasKey("Id"); - b.ToTable("EntityWithGenericKey"); + b.ToTable("EntityWithGenericKey", "DefaultSchema"); }); """, usingSystem: true), model => @@ -2701,7 +2715,7 @@ public virtual void AlternateKey_name_preserved_when_generic() b.HasAlternateKey("Property"); - b.ToTable("EntityWithGenericProperty"); + b.ToTable("EntityWithGenericProperty", "DefaultSchema"); }); """, usingSystem: true), model => @@ -2737,7 +2751,7 @@ public virtual void Discriminator_of_enum() b.HasKey("Id"); - b.ToTable("EntityWithEnumType"); + b.ToTable("EntityWithEnumType", "DefaultSchema"); b.HasDiscriminator("Day"); }); @@ -2770,7 +2784,7 @@ public virtual void Discriminator_of_enum_to_string() b.HasKey("Id"); - b.ToTable("EntityWithEnumType"); + b.ToTable("EntityWithEnumType", "DefaultSchema"); b.HasDiscriminator("Day"); }); @@ -2819,7 +2833,7 @@ public virtual void Temporal_table_information_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithStringProperty"); + b.ToTable("EntityWithStringProperty", "DefaultSchema"); b.ToTable(tb => tb.IsTemporal(ttb => { @@ -2881,11 +2895,11 @@ public virtual void Temporal_table_information_is_stored_in_snapshot_minimal_set b.HasKey("Id"); - b.ToTable("EntityWithStringProperty"); + b.ToTable("EntityWithStringProperty", "DefaultSchema"); b.ToTable(tb => tb.IsTemporal(ttb => { - ttb.UseHistoryTable("EntityWithStringPropertyHistory"); + ttb.UseHistoryTable("EntityWithStringPropertyHistory", "DefaultSchema"); ttb .HasPeriodStart("PeriodStart") .HasColumnName("PeriodStart"); @@ -2967,7 +2981,7 @@ public virtual void Owned_types_are_stored_in_snapshot() b.HasKey("Id") .HasName("PK_Custom"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); b.HasData( new @@ -2983,7 +2997,7 @@ public virtual void Owned_types_are_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithStringKey"); + b.ToTable("EntityWithStringKey", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithOneProperty", b => @@ -3010,7 +3024,7 @@ public virtual void Owned_types_are_stored_in_snapshot() SqlServerIndexBuilderExtensions.IncludeProperties(b1.HasIndex("Id"), new[] { "AlternateId" }); - b1.ToTable("EntityWithOneProperty"); + b1.ToTable("EntityWithOneProperty", "DefaultSchema"); b1.WithOwner("EntityWithOneProperty") .HasForeignKey("AlternateId") @@ -3063,7 +3077,7 @@ public virtual void Owned_types_are_stored_in_snapshot() b1.HasIndex("EntityWithStringKeyId"); - b1.ToTable("EntityWithStringProperty"); + b1.ToTable("EntityWithStringProperty", "DefaultSchema"); b1.HasOne("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithOneProperty", "EntityWithOneProperty") .WithOne() @@ -3164,7 +3178,7 @@ public virtual void Owned_types_are_stored_in_snapshot_when_excluded() b.HasData( new EntityWithOneProperty { Id = 1 }); - b.ToTable("EntityWithOneProperty", e => e.ExcludeFromMigrations()); + b.ToTable("EntityWithOneProperty", "DefaultSchema", e => e.ExcludeFromMigrations()); }); builder.Entity( @@ -3196,7 +3210,7 @@ public virtual void Owned_types_are_stored_in_snapshot_when_excluded() b.HasKey("Id") .HasName("PK_Custom"); - b.ToTable("EntityWithOneProperty", null, t => + b.ToTable("EntityWithOneProperty", "DefaultSchema", t => { t.ExcludeFromMigrations(); }); @@ -3215,7 +3229,7 @@ public virtual void Owned_types_are_stored_in_snapshot_when_excluded() b.HasKey("Id"); - b.ToTable("EntityWithStringKey", null, t => + b.ToTable("EntityWithStringKey", "DefaultSchema", t => { t.ExcludeFromMigrations(); }); @@ -3243,7 +3257,7 @@ public virtual void Owned_types_are_stored_in_snapshot_when_excluded() b1.HasIndex("Id"); - b1.ToTable("EntityWithOneProperty"); + b1.ToTable("EntityWithOneProperty", "DefaultSchema"); b1.WithOwner("EntityWithOneProperty") .HasForeignKey("AlternateId") @@ -3296,7 +3310,7 @@ public virtual void Owned_types_are_stored_in_snapshot_when_excluded() b1.HasIndex("EntityWithStringKeyId"); - b1.ToTable("EntityWithStringProperty", null, t => + b1.ToTable("EntityWithStringProperty", "DefaultSchema", t => { t.ExcludeFromMigrations(); }); @@ -3393,7 +3407,7 @@ public virtual void Shared_owned_types_are_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("Order"); + b.ToTable("Order", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+Order", b => @@ -3405,7 +3419,7 @@ public virtual void Shared_owned_types_are_stored_in_snapshot() b1.HasKey("OrderId"); - b1.ToTable("Order"); + b1.ToTable("Order", "DefaultSchema"); b1.WithOwner() .HasForeignKey("OrderId"); @@ -3420,7 +3434,7 @@ public virtual void Shared_owned_types_are_stored_in_snapshot() b2.HasKey("OrderDetailsOrderId"); - b2.ToTable("Order"); + b2.ToTable("Order", "DefaultSchema"); b2.WithOwner() .HasForeignKey("OrderDetailsOrderId"); @@ -3436,7 +3450,7 @@ public virtual void Shared_owned_types_are_stored_in_snapshot() b1.HasKey("OrderId"); - b1.ToTable("Order"); + b1.ToTable("Order", "DefaultSchema"); b1.WithOwner() .HasForeignKey("OrderId"); @@ -3451,7 +3465,7 @@ public virtual void Shared_owned_types_are_stored_in_snapshot() b2.HasKey("OrderDetailsOrderId"); - b2.ToTable("Order"); + b2.ToTable("Order", "DefaultSchema"); b2.WithOwner() .HasForeignKey("OrderDetailsOrderId"); @@ -3467,7 +3481,7 @@ public virtual void Shared_owned_types_are_stored_in_snapshot() b1.HasKey("OrderId"); - b1.ToTable("Order"); + b1.ToTable("Order", "DefaultSchema"); b1.WithOwner() .HasForeignKey("OrderId"); @@ -3482,7 +3496,7 @@ public virtual void Shared_owned_types_are_stored_in_snapshot() b2.HasKey("OrderInfoOrderId"); - b2.ToTable("Order"); + b2.ToTable("Order", "DefaultSchema"); b2.WithOwner() .HasForeignKey("OrderInfoOrderId"); @@ -3553,7 +3567,9 @@ partial class Snapshot : ModelSnapshot protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 - modelBuilder.HasAnnotation("Relational:MaxIdentifierLength", 128); + modelBuilder + .HasDefaultSchema("DefaultSchema") + .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); @@ -3567,7 +3583,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("Id"); - b.ToTable("TestOwner"); + b.ToTable("TestOwner", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+TestOwner", b => @@ -3587,7 +3603,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b1.ToTable((string)null); - b1.ToView("OwnedView", (string)null); + b1.ToView("OwnedView", "DefaultSchema"); b1.WithOwner() .HasForeignKey("TestOwnerId"); @@ -3639,7 +3655,9 @@ partial class Snapshot : ModelSnapshot protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 - modelBuilder.HasAnnotation("Relational:MaxIdentifierLength", 128); + modelBuilder + .HasDefaultSchema("DefaultSchema") + .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); @@ -3653,7 +3671,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("Id"); - b.ToTable("TestOwner"); + b.ToTable("TestOwner", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+TestOwner", b => @@ -3674,7 +3692,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b1.HasKey("TestOwnerId", "Id"); - b1.ToTable("TestOwnee", t => + b1.ToTable("TestOwnee", "DefaultSchema", t => { t.HasCheckConstraint("CK_TestOwnee_TestEnum_Enum_Constraint", "[TestEnum] IN (0, 1, 2)"); }); @@ -3741,7 +3759,7 @@ public virtual void Owned_types_mapped_to_json_are_stored_in_snapshot() b.HasKey("Id") .HasName("PK_Custom"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithOneProperty", b => @@ -3757,7 +3775,7 @@ public virtual void Owned_types_mapped_to_json_are_stored_in_snapshot() b1.HasKey("EntityWithOnePropertyId"); - b1.ToTable("EntityWithOneProperty"); + b1.ToTable("EntityWithOneProperty", "DefaultSchema"); b1.ToJson("EntityWithTwoProperties"); @@ -3771,7 +3789,7 @@ public virtual void Owned_types_mapped_to_json_are_stored_in_snapshot() b2.HasKey("EntityWithTwoPropertiesEntityWithOnePropertyId"); - b2.ToTable("EntityWithOneProperty"); + b2.ToTable("EntityWithOneProperty", "DefaultSchema"); b2.WithOwner() .HasForeignKey("EntityWithTwoPropertiesEntityWithOnePropertyId"); @@ -3790,7 +3808,7 @@ public virtual void Owned_types_mapped_to_json_are_stored_in_snapshot() b3.HasKey("EntityWithStringKeyEntityWithTwoPropertiesEntityWithOnePropertyId", "Id"); - b3.ToTable("EntityWithOneProperty"); + b3.ToTable("EntityWithOneProperty", "DefaultSchema"); b3.HasAnnotation("Relational:JsonPropertyName", "JsonProps"); @@ -3918,7 +3936,7 @@ public virtual void Property_annotations_are_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); """), o => Assert.Equal("AnnotationValue", o.GetEntityTypes().First().FindProperty("Id")["AnnotationName"]) @@ -3945,7 +3963,7 @@ public virtual void Custom_value_generator_is_ignored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); """), o => Assert.Null(o.GetEntityTypes().First().FindProperty("Id")[CoreAnnotationNames.ValueGeneratorFactory]) @@ -3972,7 +3990,7 @@ public virtual void Property_isNullable_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithStringProperty"); + b.ToTable("EntityWithStringProperty", "DefaultSchema"); }); """), o => Assert.False(o.GetEntityTypes().First().FindProperty("Name").IsNullable)); @@ -4003,7 +4021,7 @@ public virtual void Property_ValueGenerated_value_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """, usingSystem: true), o => Assert.Equal(ValueGenerated.OnAdd, o.GetEntityTypes().First().FindProperty("AlternateId").ValueGenerated)); @@ -4035,7 +4053,7 @@ public virtual void Property_ValueGenerated_non_identity() b.HasKey("Id"); - b.ToTable("EntityWithEnumType"); + b.ToTable("EntityWithEnumType", "DefaultSchema"); }); """), model => @@ -4069,7 +4087,7 @@ public virtual void Property_maxLength_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithStringProperty"); + b.ToTable("EntityWithStringProperty", "DefaultSchema"); }); """), o => Assert.Equal(100, o.GetEntityTypes().First().FindProperty("Name").GetMaxLength())); @@ -4096,7 +4114,7 @@ public virtual void Property_maximum_maxLength_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithStringProperty"); + b.ToTable("EntityWithStringProperty", "DefaultSchema"); }); """), o => Assert.Equal(-1, o.GetEntityTypes().First().FindProperty("Name").GetMaxLength())); @@ -4122,7 +4140,7 @@ public virtual void Property_unicodeness_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithStringProperty"); + b.ToTable("EntityWithStringProperty", "DefaultSchema"); }); """), o => Assert.False(o.GetEntityTypes().First().FindProperty("Name").IsUnicode())); @@ -4149,7 +4167,7 @@ public virtual void Property_fixedlengthness_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithStringProperty"); + b.ToTable("EntityWithStringProperty", "DefaultSchema"); }); """), o => Assert.True(o.GetEntityTypes().First().FindProperty("Name").IsFixedLength())); @@ -4178,7 +4196,7 @@ public virtual void Property_precision_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithDecimalProperty"); + b.ToTable("EntityWithDecimalProperty", "DefaultSchema"); }); """), o => @@ -4212,7 +4230,7 @@ public virtual void Property_precision_and_scale_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithDecimalProperty"); + b.ToTable("EntityWithDecimalProperty", "DefaultSchema"); }); """), o => @@ -4252,7 +4270,7 @@ public virtual void Many_facets_chained_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithStringProperty"); + b.ToTable("EntityWithStringProperty", "DefaultSchema"); }); """), o => @@ -4288,7 +4306,7 @@ public virtual void Property_concurrencyToken_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.True(o.GetEntityTypes().First().FindProperty("AlternateId").IsConcurrencyToken)); @@ -4318,7 +4336,7 @@ public virtual void Property_column_name_annotation_is_stored_in_snapshot_as_flu b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.Equal("CName", o.GetEntityTypes().First().FindProperty("AlternateId")["Relational:ColumnName"])); @@ -4349,7 +4367,7 @@ public virtual void Property_column_name_on_specific_table_is_stored_in_snapshot b.HasKey("Id"); - b.ToTable("BaseEntity"); + b.ToTable("BaseEntity", "DefaultSchema"); b.HasDiscriminator("Discriminator").HasValue("BaseEntity"); @@ -4373,7 +4391,7 @@ public virtual void Property_column_name_on_specific_table_is_stored_in_snapshot b.Property("Name") .HasColumnType("nvarchar(max)"); - b.ToTable("BaseEntity", t => + b.ToTable("BaseEntity", "DefaultSchema", t => { t.Property("Name") .HasColumnName("DuplicateDerivedEntity_Name"); @@ -4396,7 +4414,7 @@ public virtual void Property_column_name_on_specific_table_is_stored_in_snapshot Assert.Equal( "DuplicateDerivedEntity_Name", t.FindProperty(nameof(DuplicateDerivedEntity.Name)) - .GetColumnName(StoreObjectIdentifier.Table(nameof(BaseEntity)))); + .GetColumnName(StoreObjectIdentifier.Table(nameof(BaseEntity), "DefaultSchema"))); } ); }); @@ -4425,7 +4443,7 @@ public virtual void Property_column_type_annotation_is_stored_in_snapshot_as_flu b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.Equal("CType", o.GetEntityTypes().First().FindProperty("AlternateId")["Relational:ColumnType"])); @@ -4456,7 +4474,7 @@ public virtual void Property_default_value_annotation_is_stored_in_snapshot_as_f b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.Equal(1, o.GetEntityTypes().First().FindProperty("AlternateId")["Relational:DefaultValue"])); @@ -4487,7 +4505,7 @@ public virtual void Property_default_value_annotation_is_stored_in_snapshot_as_f b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """, usingSystem: true), @@ -4519,7 +4537,7 @@ public virtual void Property_default_value_sql_annotation_is_stored_in_snapshot_ b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.Equal(string.Empty, o.GetEntityTypes().First().FindProperty("AlternateId")["Relational:DefaultValueSql"])); @@ -4550,7 +4568,7 @@ public virtual void Property_default_value_sql_annotation_is_stored_in_snapshot_ b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.Equal("SQL", o.GetEntityTypes().First().FindProperty("AlternateId")["Relational:DefaultValueSql"])); @@ -4581,7 +4599,7 @@ public virtual void Property_computed_column_sql_annotation_is_stored_in_snapsho b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.Equal("SQL", o.GetEntityTypes().First().FindProperty("AlternateId")["Relational:ComputedColumnSql"])); @@ -4612,7 +4630,7 @@ public virtual void Property_computed_column_sql_stored_annotation_is_stored_in_ b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => @@ -4647,7 +4665,7 @@ public virtual void Property_computed_column_sql_annotation_is_stored_in_snapsho b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.Equal(string.Empty, o.GetEntityTypes().First().FindProperty("AlternateId")["Relational:ComputedColumnSql"])); @@ -4674,7 +4692,7 @@ public virtual void Property_default_value_of_enum_type_is_stored_in_snapshot_wi b.HasKey("Id"); - b.ToTable("EntityWithEnumType"); + b.ToTable("EntityWithEnumType", "DefaultSchema"); }); """), o => Assert.Equal(3L, o.GetEntityTypes().First().FindProperty("Day")["Relational:DefaultValue"])); @@ -4709,7 +4727,7 @@ public virtual void Property_enum_type_is_stored_in_snapshot_with_custom_convers b.HasKey("Id"); - b.ToTable("EntityWithEnumType"); + b.ToTable("EntityWithEnumType", "DefaultSchema"); b.HasData( new @@ -4747,7 +4765,7 @@ public virtual void Property_of_nullable_enum() b.HasKey("Id"); - b.ToTable("EntityWithNullableEnumType"); + b.ToTable("EntityWithNullableEnumType", "DefaultSchema"); }); """), o => Assert.True(o.GetEntityTypes().First().FindProperty("Day").IsNullable)); @@ -4773,7 +4791,7 @@ public virtual void Property_of_enum_to_nullable() b.HasKey("Id"); - b.ToTable("EntityWithEnumType"); + b.ToTable("EntityWithEnumType", "DefaultSchema"); }); """, usingSystem: true), o => Assert.False(o.GetEntityTypes().First().FindProperty("Day").IsNullable)); @@ -4798,7 +4816,7 @@ public virtual void Property_of_nullable_enum_to_string() b.HasKey("Id"); - b.ToTable("EntityWithNullableEnumType"); + b.ToTable("EntityWithNullableEnumType", "DefaultSchema"); }); """), o => Assert.True(o.GetEntityTypes().First().FindProperty("Day").IsNullable)); @@ -4830,7 +4848,7 @@ public virtual void Property_multiple_annotations_are_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => @@ -4860,7 +4878,7 @@ public virtual void Property_without_column_type() b.HasKey("Id"); - b.ToTable("Buildings"); + b.ToTable("Buildings", "DefaultSchema"); }); }, AddBoilerPlate( @@ -4876,7 +4894,7 @@ public virtual void Property_without_column_type() b.HasKey("Id"); - b.ToTable("Buildings", (string)null); + b.ToTable("Buildings", "DefaultSchema"); }); """), o => @@ -4897,7 +4915,7 @@ public virtual void Property_with_identity_column() b.HasKey("Id"); - b.ToTable("Buildings"); + b.ToTable("Buildings", "DefaultSchema"); }); }, AddBoilerPlate( @@ -4913,7 +4931,7 @@ public virtual void Property_with_identity_column() b.HasKey("Id"); - b.ToTable("Buildings", (string)null); + b.ToTable("Buildings", "DefaultSchema"); }); """), o => @@ -4936,7 +4954,7 @@ public virtual void Property_with_identity_column_custom_seed() b.HasKey("Id"); - b.ToTable("Buildings"); + b.ToTable("Buildings", "DefaultSchema"); }); }, AddBoilerPlate( @@ -4952,7 +4970,7 @@ public virtual void Property_with_identity_column_custom_seed() b.HasKey("Id"); - b.ToTable("Buildings", (string)null); + b.ToTable("Buildings", "DefaultSchema"); }); """), o => @@ -4975,7 +4993,7 @@ public virtual void Property_with_identity_column_custom_increment() b.HasKey("Id"); - b.ToTable("Buildings"); + b.ToTable("Buildings", "DefaultSchema"); }); }, AddBoilerPlate( @@ -4991,7 +5009,7 @@ public virtual void Property_with_identity_column_custom_increment() b.HasKey("Id"); - b.ToTable("Buildings", (string)null); + b.ToTable("Buildings", "DefaultSchema"); }); """), o => @@ -5014,7 +5032,7 @@ public virtual void Property_with_identity_column_custom_seed_increment() b.HasKey("Id"); - b.ToTable("Buildings"); + b.ToTable("Buildings", "DefaultSchema"); }); }, AddBoilerPlate( @@ -5030,7 +5048,7 @@ public virtual void Property_with_identity_column_custom_seed_increment() b.HasKey("Id"); - b.ToTable("Buildings", (string)null); + b.ToTable("Buildings", "DefaultSchema"); }); """), o => @@ -5066,7 +5084,7 @@ public virtual void Property_column_order_annotation_is_stored_in_snapshot_as_fl b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.Equal(1, o.GetEntityTypes().First().FindProperty("AlternateId").GetColumnOrder())); @@ -5077,7 +5095,9 @@ public virtual void SQLServer_model_legacy_identity_seed_int_annotation() builder => builder.HasAnnotation(SqlServerAnnotationNames.IdentitySeed, 8), AddBoilerPlate( """ - modelBuilder.HasAnnotation("Relational:MaxIdentifierLength", 128); + modelBuilder + .HasDefaultSchema("DefaultSchema") + .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 8L); """), @@ -5108,7 +5128,7 @@ public virtual void SQLServer_property_legacy_identity_seed_int_annotation() b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.Equal(8L, o.GetEntityTypes().First().FindProperty("Id").GetIdentitySeed())); @@ -5145,7 +5165,7 @@ public virtual void Key_annotations_are_stored_in_snapshot() b.HasAlternateKey("AlternateId") .HasAnnotation("AnnotationName", "AnnotationValue"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.Equal( @@ -5174,7 +5194,7 @@ public virtual void Key_Fluent_APIs_are_properly_generated() SqlServerKeyBuilderExtensions.IsClustered(b.HasKey("Id")); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); """), o => Assert.True(o.GetEntityTypes().First().GetKeys().Single(k => k.IsPrimaryKey()).IsClustered())); @@ -5206,7 +5226,7 @@ public virtual void Key_name_annotation_is_stored_in_snapshot_as_fluent_api() b.HasAlternateKey("AlternateId") .HasName("KeyName"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.Equal( @@ -5241,7 +5261,7 @@ public virtual void Key_multiple_annotations_are_stored_in_snapshot() .HasName("IndexName") .HasAnnotation("AnnotationName", "AnnotationValue"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => @@ -5284,7 +5304,7 @@ public virtual void Index_annotations_are_stored_in_snapshot() b.HasIndex("AlternateId") .HasAnnotation("AnnotationName", "AnnotationValue"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.Equal("AnnotationValue", o.GetEntityTypes().First().GetIndexes().First()["AnnotationName"])); @@ -5317,7 +5337,7 @@ public virtual void Index_Fluent_APIs_are_properly_generated() SqlServerIndexBuilderExtensions.IsClustered(b.HasIndex("AlternateId")); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.True(o.GetEntityTypes().Single().GetIndexes().Single().IsClustered())); @@ -5349,7 +5369,7 @@ public virtual void Index_IsUnique_is_stored_in_snapshot() b.HasIndex("AlternateId") .IsUnique(); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.True(o.GetEntityTypes().First().GetIndexes().First().IsUnique)); @@ -5438,7 +5458,7 @@ public virtual void Index_IsDescending_is_stored_in_snapshot() b.HasIndex(new[] { "X", "Y", "Z" }, "IX_unspecified"); - b.ToTable("EntityWithThreeProperties"); + b.ToTable("EntityWithThreeProperties", "DefaultSchema"); }); """), o => @@ -5491,7 +5511,7 @@ public virtual void Index_database_name_annotation_is_stored_in_snapshot_as_flue b.HasIndex("AlternateId") .HasDatabaseName("IndexName"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => @@ -5529,7 +5549,7 @@ public virtual void Index_filter_is_stored_in_snapshot() b.HasIndex("AlternateId") .HasFilter("AlternateId <> 0"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => Assert.Equal( @@ -5564,7 +5584,7 @@ public virtual void Index_multiple_annotations_are_stored_in_snapshot() b.HasIndex(new[] { "AlternateId" }, "IndexName") .HasAnnotation("AnnotationName", "AnnotationValue"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); """), o => @@ -5608,7 +5628,7 @@ public virtual void Index_with_default_constraint_name_exceeding_max() b.HasIndex("SomePropertyWithAnExceedinglyLongIdentifierThatCausesTheDefaultIndexNameToExceedTheMaximumIdentifierLimit"); - b.ToTable("EntityWithStringProperty"); + b.ToTable("EntityWithStringProperty", "DefaultSchema"); }); """), model => Assert.Equal(128, model.GetEntityTypes().First().GetIndexes().First().GetDatabaseName().Length)); @@ -5638,7 +5658,7 @@ public virtual void IndexAttribute_causes_column_to_have_key_or_index_column_len b.HasIndex("FirstName", "LastName"); - b.ToTable("EntityWithIndexAttribute"); + b.ToTable("EntityWithIndexAttribute", "DefaultSchema"); }); """), model => @@ -5681,7 +5701,7 @@ public virtual void IndexAttribute_name_is_stored_in_snapshot() b.HasIndex(new[] { "FirstName", "LastName" }, "NamedIndex"); - b.ToTable("EntityWithNamedIndexAttribute"); + b.ToTable("EntityWithNamedIndexAttribute", "DefaultSchema"); }); """), model => @@ -5730,7 +5750,7 @@ public virtual void IndexAttribute_IsUnique_is_stored_in_snapshot() .IsUnique() .HasFilter("[FirstName] IS NOT NULL AND [LastName] IS NOT NULL"); - b.ToTable("EntityWithUniqueIndexAttribute"); + b.ToTable("EntityWithUniqueIndexAttribute", "DefaultSchema"); }); """), model => @@ -5780,7 +5800,7 @@ public virtual void IndexAttribute_IncludeProperties_generated_without_fluent_ap SqlServerIndexBuilderExtensions.IncludeProperties(b.HasIndex("Id"), new[] { "Name" }); - b.ToTable("EntityWithStringProperty"); + b.ToTable("EntityWithStringProperty", "DefaultSchema"); }); """), model => @@ -5817,7 +5837,7 @@ public virtual void ForeignKey_annotations_are_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -5836,7 +5856,7 @@ public virtual void ForeignKey_annotations_are_stored_in_snapshot() b.HasIndex("AlternateId") .IsUnique(); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -5881,7 +5901,7 @@ public virtual void ForeignKey_isRequired_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithStringKey"); + b.ToTable("EntityWithStringKey", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithStringProperty", b => @@ -5901,7 +5921,7 @@ public virtual void ForeignKey_isRequired_is_stored_in_snapshot() b.HasIndex("Name") .IsUnique(); - b.ToTable("EntityWithStringProperty"); + b.ToTable("EntityWithStringProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithStringProperty", b => @@ -5935,7 +5955,7 @@ public virtual void ForeignKey_isUnique_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithStringKey"); + b.ToTable("EntityWithStringKey", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithStringProperty", b => @@ -5953,7 +5973,7 @@ public virtual void ForeignKey_isUnique_is_stored_in_snapshot() b.HasIndex("Name"); - b.ToTable("EntityWithStringProperty"); + b.ToTable("EntityWithStringProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithStringProperty", b => @@ -5998,7 +6018,7 @@ public virtual void ForeignKey_with_non_primary_principal_is_stored_in_snapshot( b.HasKey("Id"); - b.ToTable("EntityWithStringAlternateKey"); + b.ToTable("EntityWithStringAlternateKey", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithStringProperty", b => @@ -6016,7 +6036,7 @@ public virtual void ForeignKey_with_non_primary_principal_is_stored_in_snapshot( b.HasIndex("Name"); - b.ToTable("EntityWithStringProperty"); + b.ToTable("EntityWithStringProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithStringProperty", b => @@ -6055,7 +6075,7 @@ public virtual void ForeignKey_deleteBehavior_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -6071,7 +6091,7 @@ public virtual void ForeignKey_deleteBehavior_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithOneProperty", b => @@ -6108,7 +6128,7 @@ public virtual void ForeignKey_deleteBehavior_is_stored_in_snapshot_for_one_to_o b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -6124,7 +6144,7 @@ public virtual void ForeignKey_deleteBehavior_is_stored_in_snapshot_for_one_to_o b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithOneProperty", b => @@ -6170,7 +6190,7 @@ public virtual void ForeignKey_name_preserved_when_generic() b.HasKey("Id"); - b.ToTable("EntityWithGenericKey"); + b.ToTable("EntityWithGenericKey", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithGenericProperty", b => @@ -6188,7 +6208,7 @@ public virtual void ForeignKey_name_preserved_when_generic() b.HasIndex("Property"); - b.ToTable("EntityWithGenericProperty"); + b.ToTable("EntityWithGenericProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithGenericProperty", b => @@ -6252,7 +6272,7 @@ public virtual void ForeignKey_constraint_name_is_stored_in_snapshot_as_fluent_a b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -6271,7 +6291,7 @@ public virtual void ForeignKey_constraint_name_is_stored_in_snapshot_as_fluent_a b.HasIndex("AlternateId") .IsUnique(); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -6319,7 +6339,7 @@ public virtual void ForeignKey_multiple_annotations_are_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -6338,7 +6358,7 @@ public virtual void ForeignKey_multiple_annotations_are_stored_in_snapshot() b.HasIndex("AlternateId") .IsUnique(); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -6399,7 +6419,7 @@ public virtual void Do_not_generate_entity_type_builder_again_if_no_foreign_key_ b.HasIndex("NavigationId"); - b.ToTable("BaseType"); + b.ToTable("BaseType", "DefaultSchema"); b.HasDiscriminator("Discriminator").HasValue("BaseType"); @@ -6416,7 +6436,7 @@ public virtual void Do_not_generate_entity_type_builder_again_if_no_foreign_key_ b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+DerivedType", b => @@ -6458,7 +6478,7 @@ public virtual void ForeignKey_principal_key_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -6474,7 +6494,7 @@ public virtual void ForeignKey_principal_key_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithOneProperty", b => @@ -6523,7 +6543,7 @@ public virtual void ForeignKey_principal_key_with_non_default_name_is_stored_in_ b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -6542,7 +6562,7 @@ public virtual void ForeignKey_principal_key_with_non_default_name_is_stored_in_ b.HasAlternateKey("AlternateId") .HasAnnotation("Name", "Value"); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithOneProperty", b => @@ -6600,7 +6620,7 @@ public virtual void Navigation_annotations_are_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -6619,7 +6639,7 @@ public virtual void Navigation_annotations_are_stored_in_snapshot() b.HasIndex("AlternateId") .IsUnique(); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -6668,7 +6688,7 @@ public virtual void Navigation_isRequired_is_stored_in_snapshot() b.HasKey("Id"); - b.ToTable("EntityWithOneProperty"); + b.ToTable("EntityWithOneProperty", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -6687,7 +6707,7 @@ public virtual void Navigation_isRequired_is_stored_in_snapshot() b.HasIndex("AlternateId") .IsUnique(); - b.ToTable("EntityWithTwoProperties"); + b.ToTable("EntityWithTwoProperties", "DefaultSchema"); }); modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithTwoProperties", b => @@ -6872,7 +6892,9 @@ partial class Snapshot : ModelSnapshot protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 - modelBuilder.HasAnnotation("Relational:MaxIdentifierLength", 128); + modelBuilder + .HasDefaultSchema("DefaultSchema") + .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); @@ -7010,7 +7032,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("Id"); - b.ToTable("EntityWithManyProperties"); + b.ToTable("EntityWithManyProperties", "DefaultSchema"); b.HasData( new @@ -7220,7 +7242,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) protected virtual string GetHeading(bool empty = false) => """ - modelBuilder.HasAnnotation("Relational:MaxIdentifierLength", 128); + modelBuilder + .HasDefaultSchema("DefaultSchema") + .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); @@ -7271,6 +7295,7 @@ protected void Test(Action buildModel, string expectedCode, Action protected void Test(Action buildModel, string expectedCode, Action assert) { var modelBuilder = CreateConventionalModelBuilder(); + modelBuilder.HasDefaultSchema("DefaultSchema"); modelBuilder.HasChangeTrackingStrategy(ChangeTrackingStrategy.Snapshot); modelBuilder.Model.RemoveAnnotation(CoreAnnotationNames.ProductVersion); buildModel(modelBuilder); From 3a84b1d03c2fc13d5197dd92f6859a3b2de17b57 Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Tue, 7 Feb 2023 11:27:03 +0000 Subject: [PATCH 2/2] Andriy's suggestion --- .../Extensions/RelationalEntityTypeExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs b/src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs index 7d859cba190..b0ea10a4f36 100644 --- a/src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs +++ b/src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs @@ -145,8 +145,8 @@ public static void SetTableName(this IMutableEntityType entityType, string? name return (string?)schemaAnnotation.Value ?? GetDefaultSchema(entityType); } - return entityType.BaseType != null - ? entityType.GetRootType().GetSchema() ?? GetDefaultSchema(entityType) + return entityType.BaseType != null && entityType.BaseType.GetTableName() != null + ? entityType.BaseType.GetSchema() : GetDefaultSchema(entityType); }