/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public override IEnumerable <IAnnotation> For(IRelationalModel model, bool designTime) { if (!designTime) { yield break; } var maxSize = model.Model.GetDatabaseMaxSize(); var serviceTier = model.Model.GetServiceTierSql(); var performanceLevel = model.Model.GetPerformanceLevelSql(); if (maxSize != null || serviceTier != null || performanceLevel != null) { var options = new StringBuilder(); if (maxSize != null) { options.Append("MAXSIZE = "); options.Append(maxSize); options.Append(", "); } if (serviceTier != null) { options.Append("EDITION = "); options.Append(serviceTier); options.Append(", "); } if (performanceLevel != null) { options.Append("SERVICE_OBJECTIVE = "); options.Append(performanceLevel); options.Append(", "); } options.Remove(options.Length - 2, 2); yield return(new Annotation(SqlServerAnnotationNames.EditionOptions, options.ToString())); } if (model.Tables.Any(t => !t.IsExcludedFromMigrations && (t[SqlServerAnnotationNames.MemoryOptimized] as bool? == true))) { yield return(new Annotation(SqlServerAnnotationNames.MemoryOptimized, true)); } }
public static string ToDebugString( [NotNull] this IRelationalModel model, MetadataDebugStringOptions options, int indent = 0) { var builder = new StringBuilder(); var indentString = new string(' ', indent); builder.Append(indentString).Append("RelationalModel: "); if (model.Collation != null) { builder.AppendLine().Append(indentString).Append("Collation: " + model.Collation); } foreach (var table in model.Tables) { builder.AppendLine().Append(table.ToDebugString(options, indent + 2)); } foreach (var view in model.Views) { builder.AppendLine().Append(view.ToDebugString(options, indent + 2)); } foreach (var function in model.Functions) { builder.AppendLine().Append(function.ToDebugString(options, indent + 2)); } foreach (var query in model.Queries) { builder.AppendLine().Append(query.ToDebugString(options, indent + 2)); } foreach (var sequence in model.Sequences) { builder.AppendLine().Append(sequence.ToDebugString(options, indent + 2)); } if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) { builder.Append(model.AnnotationsToDebugString(indent)); } return(builder.ToString()); }
public override IReadOnlyList <MigrationOperation> GetDifferences(IRelationalModel source, IRelationalModel target) { var shardingOption = Cache.ServiceProvider.GetService <IOptions <EFCoreShardingOptions> >().Value; var sourceOperations = base.GetDifferences(source, target).ToList(); //忽略外键 if (shardingOption.MigrationsWithoutForeignKey) { sourceOperations.RemoveAll(x => x is AddForeignKeyOperation || x is DropForeignKeyOperation); foreach (var operation in sourceOperations.OfType <CreateTableOperation>()) { operation.ForeignKeys?.Clear(); } } return(sourceOperations); }
public override IEnumerable <IAnnotation> For(IRelationalModel model) { if (GetActualModelCharSet(model.Model, DelegationModes.ApplyToDatabases) is string charSet) { yield return(new Annotation( MySqlAnnotationNames.CharSet, charSet)); } // If a collation delegation modes has been set, but does not contain DelegationMode.ApplyToDatabase, we reset the EF Core // handled Collation property in MySqlMigrationsModelDiffer. // Handle other annotations (including the delegation annotations). foreach (var annotation in model.Model.GetAnnotations() .Where(a => a.Name is MySqlAnnotationNames.CharSetDelegation or MySqlAnnotationNames.CollationDelegation)) { yield return(annotation); } }
/// <inheritdoc /> public override IEnumerable <IAnnotation> ForRemove(IRelationalModel model) => model.GetAnnotations().Where(a => a.Name != SqlServerAnnotationNames.EditionOptions);
private static void AssertDefaultMappings(IRelationalModel model) { var orderType = model.Model.FindEntityType(typeof(Order)); var orderMapping = orderType.GetDefaultMappings().Single(); Assert.True(orderMapping.IncludesDerivedTypes); Assert.Equal( new[] { nameof(Order.Id), nameof(Order.AlternateId), nameof(Order.CustomerId), nameof(Order.OrderDate) }, orderMapping.ColumnMappings.Select(m => m.Property.Name)); var ordersTable = orderMapping.Table; Assert.Equal(new[] { nameof(Order) }, ordersTable.EntityTypeMappings.Select(m => m.EntityType.DisplayName())); Assert.Equal( new[] { nameof(Order.AlternateId), nameof(Order.CustomerId), nameof(Order.Id), "OrderDate" }, ordersTable.Columns.Select(m => m.Name)); Assert.Equal("Order", ordersTable.Name); Assert.Null(ordersTable.Schema); var orderDate = orderType.FindProperty(nameof(Order.OrderDate)); var orderDateMapping = orderDate.GetDefaultColumnMappings().Single(); Assert.NotNull(orderDateMapping.TypeMapping); Assert.Equal("default_datetime_mapping", orderDateMapping.TypeMapping.StoreType); Assert.Same(orderMapping, orderDateMapping.TableMapping); var orderDetailsOwnership = orderType.FindNavigation(nameof(Order.Details)).ForeignKey; var orderDetailsType = orderDetailsOwnership.DeclaringEntityType; var orderDetailsTable = orderDetailsType.GetDefaultMappings().Single().Table; Assert.NotEqual(ordersTable, orderDetailsTable); Assert.Empty(ordersTable.GetReferencingRowInternalForeignKeys(orderType)); var orderDetailsDate = orderDetailsType.FindProperty(nameof(OrderDetails.OrderDate)); Assert.Equal(new[] { orderDetailsDate }, orderDetailsTable.FindColumn("OrderDate").PropertyMappings.Select(m => m.Property)); var orderDateColumn = orderDateMapping.Column; Assert.Same(orderDateColumn, ordersTable.FindColumn("OrderDate")); Assert.Same(orderDateColumn, ordersTable.FindColumn(orderDate)); Assert.Equal(new[] { orderDate }, orderDateColumn.PropertyMappings.Select(m => m.Property)); Assert.Equal("OrderDate", orderDateColumn.Name); Assert.Equal("default_datetime_mapping", orderDateColumn.StoreType); Assert.False(orderDateColumn.IsNullable); Assert.Same(ordersTable, orderDateColumn.Table); var customerType = model.Model.FindEntityType(typeof(Customer)); var customerTable = customerType.GetDefaultMappings().Single().Table; Assert.Equal("Customer", customerTable.Name); Assert.Null(customerTable.Schema); var specialCustomerType = model.Model.FindEntityType(typeof(SpecialCustomer)); var customerPk = specialCustomerType.FindPrimaryKey(); var specialCustomerDefaultMapping = specialCustomerType.GetDefaultMappings().Single(); Assert.True(specialCustomerDefaultMapping.IsSplitEntityTypePrincipal); Assert.True(specialCustomerDefaultMapping.IncludesDerivedTypes); var specialCustomerTable = specialCustomerDefaultMapping.Table; Assert.Equal(customerTable, specialCustomerTable); Assert.Equal(2, specialCustomerTable.EntityTypeMappings.Count()); Assert.True(specialCustomerTable.EntityTypeMappings.First().IsSharedTablePrincipal); Assert.Equal(specialCustomerType.GetDiscriminatorProperty() == null ? 8 : 9, specialCustomerTable.Columns.Count()); var specialityColumn = specialCustomerTable.Columns.Single(c => c.Name == nameof(SpecialCustomer.Speciality)); Assert.Equal(specialCustomerType.GetDiscriminatorProperty() != null, specialityColumn.IsNullable); }
private static void AssertViews(IRelationalModel model) { var orderType = model.Model.FindEntityType(typeof(Order)); var orderMapping = orderType.GetViewMappings().Single(); Assert.Same(orderType.GetViewMappings(), orderType.GetViewOrTableMappings()); Assert.True(orderMapping.IncludesDerivedTypes); Assert.Equal( new[] { nameof(Order.CustomerId), nameof(Order.OrderDate), nameof(Order.OrderId) }, orderMapping.ColumnMappings.Select(m => m.Property.Name)); var ordersView = orderMapping.View; Assert.Same(ordersView, model.FindView(ordersView.Name, ordersView.Schema)); Assert.Equal( new[] { "OrderDetails.BillingAddress#Address", "OrderDetails.ShippingAddress#Address", nameof(Order), nameof(OrderDetails) }, ordersView.EntityTypeMappings.Select(m => m.EntityType.DisplayName())); Assert.Equal(new[] { nameof(Order.CustomerId), "Details_BillingAddress_City", "Details_BillingAddress_Street", "Details_ShippingAddress_City", "Details_ShippingAddress_Street", "OrderDateView", nameof(Order.OrderId) }, ordersView.Columns.Select(m => m.Name)); Assert.Equal("OrderView", ordersView.Name); Assert.Equal("viewSchema", ordersView.Schema); Assert.Null(ordersView.ViewDefinition); var orderDate = orderType.FindProperty(nameof(Order.OrderDate)); Assert.False(orderDate.IsViewColumnNullable()); var orderDateMapping = orderDate.GetViewColumnMappings().Single(); Assert.NotNull(orderDateMapping.TypeMapping); Assert.Equal("default_datetime_mapping", orderDateMapping.TypeMapping.StoreType); Assert.Same(orderMapping, orderDateMapping.ViewMapping); var orderDetailsOwnership = orderType.FindNavigation(nameof(Order.Details)).ForeignKey; var orderDetailsType = orderDetailsOwnership.DeclaringEntityType; Assert.Same(ordersView, orderDetailsType.GetViewMappings().Single().View); Assert.Equal(ordersView.GetReferencingInternalForeignKeys(orderType), ordersView.GetInternalForeignKeys(orderDetailsType)); var orderDetailsDate = orderDetailsType.FindProperty(nameof(OrderDetails.OrderDate)); Assert.True(orderDetailsDate.IsViewColumnNullable()); var orderDateColumn = orderDateMapping.Column; Assert.Same(orderDateColumn, ordersView.FindColumn("OrderDateView")); Assert.Same(orderDateColumn, orderDate.FindViewColumn(ordersView.Name, ordersView.Schema)); Assert.Equal(new[] { orderDate, orderDetailsDate }, orderDateColumn.PropertyMappings.Select(m => m.Property)); Assert.Equal("OrderDateView", orderDateColumn.Name); Assert.Equal("default_datetime_mapping", orderDateColumn.StoreType); Assert.False(orderDateColumn.IsNullable); Assert.Same(ordersView, orderDateColumn.Table); var customerType = model.Model.FindEntityType(typeof(Customer)); var customerView = customerType.GetViewMappings().Single().Table; Assert.Equal("CustomerView", customerView.Name); Assert.Equal("viewSchema", customerView.Schema); var specialCustomerType = model.Model.FindEntityType(typeof(SpecialCustomer)); Assert.Same(customerView, specialCustomerType.GetViewMappings().Single().Table); }
private static void AssertTables(IRelationalModel model, Mapping mapping) { var orderType = model.Model.FindEntityType(typeof(Order)); var orderMapping = orderType.GetTableMappings().Single(); Assert.True(orderMapping.IncludesDerivedTypes); Assert.Equal( new[] { nameof(Order.AlternateId), nameof(Order.CustomerId), nameof(Order.OrderDate), nameof(Order.OrderId) }, orderMapping.ColumnMappings.Select(m => m.Property.Name)); var ordersTable = orderMapping.Table; Assert.Same(ordersTable, model.FindTable(ordersTable.Name, ordersTable.Schema)); Assert.Equal( new[] { "OrderDetails.BillingAddress#Address", "OrderDetails.ShippingAddress#Address", nameof(Order), nameof(OrderDetails) }, ordersTable.EntityTypeMappings.Select(m => m.EntityType.DisplayName())); Assert.Equal(new[] { nameof(Order.AlternateId), nameof(Order.CustomerId), "Details_BillingAddress_City", "Details_BillingAddress_Street", "Details_ShippingAddress_City", "Details_ShippingAddress_Street", nameof(Order.OrderDate), nameof(Order.OrderId) }, ordersTable.Columns.Select(m => m.Name)); Assert.Equal("Order", ordersTable.Name); Assert.Null(ordersTable.Schema); Assert.False(ordersTable.IsExcludedFromMigrations); Assert.True(ordersTable.IsShared); var orderDate = orderType.FindProperty(nameof(Order.OrderDate)); Assert.False(orderDate.IsColumnNullable()); var orderDateMapping = orderDate.GetTableColumnMappings().Single(); Assert.NotNull(orderDateMapping.TypeMapping); Assert.Equal("default_datetime_mapping", orderDateMapping.TypeMapping.StoreType); Assert.Same(orderMapping, orderDateMapping.TableMapping); var orderDateColumn = orderDateMapping.Column; Assert.Same(orderDateColumn, ordersTable.FindColumn("OrderDate")); Assert.Same(orderDateColumn, orderDate.FindTableColumn(ordersTable.Name, ordersTable.Schema)); Assert.Equal("OrderDate", orderDateColumn.Name); Assert.Equal("default_datetime_mapping", orderDateColumn.StoreType); Assert.False(orderDateColumn.IsNullable); Assert.Same(ordersTable, orderDateColumn.Table); var orderPk = orderType.FindPrimaryKey(); var orderPkConstraint = orderPk.GetMappedConstraints().Single(); Assert.Equal("PK_Order", orderPkConstraint.Name); Assert.Equal(nameof(Order.OrderId), orderPkConstraint.Columns.Single().Name); Assert.Same(ordersTable, orderPkConstraint.Table); Assert.True(orderPkConstraint.IsPrimaryKey); Assert.Same(orderPkConstraint, ordersTable.UniqueConstraints.Last()); Assert.Same(orderPkConstraint, ordersTable.PrimaryKey); var orderAk = orderType.GetKeys().Single(k => k != orderPk); var orderAkConstraint = orderAk.GetMappedConstraints().Single(); Assert.Equal("AK_AlternateId", orderAkConstraint.Name); Assert.Equal(nameof(Order.AlternateId), orderAkConstraint.Columns.Single().Name); Assert.Same(ordersTable, orderAkConstraint.Table); Assert.False(orderAkConstraint.IsPrimaryKey); Assert.Same(orderAkConstraint, ordersTable.UniqueConstraints.First()); var orderDateIndex = orderType.GetIndexes().Single(i => i.Properties.Any(p => p.Name == nameof(Order.OrderDate))); var orderDateTableIndex = orderDateIndex.GetMappedTableIndexes().Single(); Assert.Equal("IX_OrderDate", orderDateTableIndex.Name); Assert.Equal(nameof(Order.OrderDate), orderDateTableIndex.Columns.Single().Name); Assert.Same(ordersTable, orderDateTableIndex.Table); Assert.True(orderDateTableIndex.IsUnique); Assert.Null(orderDateTableIndex.Filter); Assert.Equal(orderDateTableIndex, ordersTable.Indexes.Last()); var orderCustomerIndex = orderType.GetIndexes().Single(i => i.Properties.Any(p => p.Name == nameof(Order.CustomerId))); var orderTableIndex = orderCustomerIndex.GetMappedTableIndexes().Single(); Assert.Equal("IX_Order_CustomerId", orderTableIndex.Name); Assert.Equal(nameof(Order.CustomerId), orderTableIndex.Columns.Single().Name); Assert.Same(ordersTable, orderTableIndex.Table); Assert.False(orderTableIndex.IsUnique); Assert.Null(orderTableIndex.Filter); Assert.Equal(orderCustomerIndex, orderTableIndex.MappedIndexes.Single()); Assert.Same(orderTableIndex, ordersTable.Indexes.First()); var orderDateFk = orderType.GetForeignKeys().Single(fk => fk.PrincipalEntityType.ClrType == typeof(DateDetails)); var orderDateFkConstraint = orderDateFk.GetMappedConstraints().Single(); Assert.Equal("FK_DateDetails", orderDateFkConstraint.Name); Assert.Equal(nameof(Order.OrderDate), orderDateFkConstraint.Columns.Single().Name); Assert.Equal(nameof(DateDetails.Date), orderDateFkConstraint.PrincipalColumns.Single().Name); Assert.Equal("DateDetails", orderDateFkConstraint.PrincipalTable.Name); var orderCustomerFk = orderType.GetForeignKeys().Single(fk => fk.PrincipalEntityType.ClrType == typeof(Customer)); var orderCustomerFkConstraint = orderCustomerFk.GetMappedConstraints().Single(); Assert.Equal("FK_Order_Customer_CustomerId", orderCustomerFkConstraint.Name); Assert.Equal(nameof(Order.CustomerId), orderCustomerFkConstraint.Columns.Single().Name); Assert.Equal(nameof(Customer.Id), orderCustomerFkConstraint.PrincipalColumns.Single().Name); Assert.Same(ordersTable, orderCustomerFkConstraint.Table); Assert.Equal("Customer", orderCustomerFkConstraint.PrincipalTable.Name); Assert.Equal(ReferentialAction.Cascade, orderCustomerFkConstraint.OnDeleteAction); Assert.Equal(orderCustomerFk, orderCustomerFkConstraint.MappedForeignKeys.Single()); Assert.Equal(new[] { orderDateFkConstraint, orderCustomerFkConstraint }, ordersTable.ForeignKeyConstraints); var orderDetailsOwnership = orderType.FindNavigation(nameof(Order.Details)).ForeignKey; var orderDetailsType = orderDetailsOwnership.DeclaringEntityType; Assert.Same(ordersTable, orderDetailsType.GetTableMappings().Single().Table); Assert.Equal(ordersTable.GetReferencingRowInternalForeignKeys(orderType), ordersTable.GetRowInternalForeignKeys(orderDetailsType)); Assert.Empty(orderDetailsOwnership.GetMappedConstraints()); Assert.Equal(2, orderDetailsType.GetForeignKeys().Count()); var orderDetailsDateIndex = orderDetailsType.GetIndexes().Single(i => i.Properties.Any(p => p.Name == nameof(Order.OrderDate))); var orderDetailsDateTableIndex = orderDetailsDateIndex.GetMappedTableIndexes().Single(); Assert.Same(orderDateTableIndex, orderDetailsDateTableIndex); Assert.Equal(new[] { orderDateIndex, orderDetailsDateIndex }, orderDateTableIndex.MappedIndexes); var orderDetailsPk = orderDetailsType.FindPrimaryKey(); Assert.Same(orderPkConstraint, orderDetailsPk.GetMappedConstraints().Single()); var billingAddressOwnership = orderDetailsType.FindNavigation(nameof(OrderDetails.BillingAddress)).ForeignKey; var billingAddressType = billingAddressOwnership.DeclaringEntityType; var shippingAddressOwnership = orderDetailsType.FindNavigation(nameof(OrderDetails.ShippingAddress)).ForeignKey; var shippingAddressType = shippingAddressOwnership.DeclaringEntityType; Assert.Equal( new[] { billingAddressType.FindPrimaryKey(), shippingAddressType.FindPrimaryKey(), orderPk, orderDetailsPk }, orderPkConstraint.MappedKeys); var orderDetailsDate = orderDetailsType.FindProperty(nameof(OrderDetails.OrderDate)); Assert.True(orderDetailsDate.IsColumnNullable()); Assert.Equal(new[] { orderDate, orderDetailsDate }, orderDateColumn.PropertyMappings.Select(m => m.Property)); var orderDetailsAk = orderDetailsType.GetKeys().Single(k => k != orderDetailsPk); var orderDetailsAkConstraint = orderDetailsAk.GetMappedConstraints().Single(); Assert.Same(orderAkConstraint, orderDetailsAkConstraint); Assert.Equal(new[] { orderAk, orderDetailsAk }, orderAkConstraint.MappedKeys); var orderDetailsDateFk = orderDetailsType.GetForeignKeys().Single(fk => fk.PrincipalEntityType.ClrType == typeof(DateDetails)); var orderDetailsDateFkConstraint = orderDateFk.GetMappedConstraints().Single(); Assert.Same(orderDateFkConstraint, orderDetailsDateFkConstraint); Assert.Equal(new[] { orderDateFk, orderDetailsDateFk }, orderDateFkConstraint.MappedForeignKeys); Assert.Equal("FK_DateDetails", orderDateFkConstraint.Name); var customerType = model.Model.FindEntityType(typeof(Customer)); var customerTable = customerType.GetTableMappings().Single().Table; Assert.Equal("Customer", customerTable.Name); var specialCustomerType = model.Model.FindEntityType(typeof(SpecialCustomer)); var customerPk = specialCustomerType.FindPrimaryKey(); if (mapping == Mapping.TPT) { Assert.Equal(2, specialCustomerType.GetTableMappings().Count()); Assert.True(specialCustomerType.GetTableMappings().First().IsMainTableMapping); Assert.False(specialCustomerType.GetTableMappings().Last().IsMainTableMapping); var specialCustomerTable = specialCustomerType.GetTableMappings().Select(t => t.Table).First(t => t.Name == "SpecialCustomer"); Assert.Equal("SpecialSchema", specialCustomerTable.Schema); Assert.Equal(3, specialCustomerTable.Columns.Count()); Assert.True(specialCustomerTable.EntityTypeMappings.Single().IsMainEntityTypeMapping); var specialityColumn = specialCustomerTable.Columns.Single(c => c.Name == nameof(SpecialCustomer.Speciality)); Assert.False(specialityColumn.IsNullable); Assert.Equal(2, customerPk.GetMappedConstraints().Count()); var specialCustomerPkConstraint = specialCustomerTable.PrimaryKey; Assert.Equal("PK_SpecialCustomer", specialCustomerPkConstraint.Name); Assert.Same(specialCustomerPkConstraint.MappedKeys.Single(), customerPk); var idProperty = customerPk.Properties.Single(); Assert.Equal(3, idProperty.GetTableColumnMappings().Count()); Assert.Empty(customerTable.ForeignKeyConstraints); var specialCustomerUniqueConstraint = customerTable.UniqueConstraints.Single(c => !c.IsPrimaryKey); Assert.Equal("AK_Customer_SpecialityAk", specialCustomerUniqueConstraint.Name); Assert.NotNull(specialCustomerUniqueConstraint.MappedKeys.Single()); var specialCustomerFkConstraint = specialCustomerTable.ForeignKeyConstraints.Single(); Assert.Equal("FK_SpecialCustomer_Customer_RelatedCustomerSpeciality", specialCustomerFkConstraint.Name); Assert.Same(customerTable, specialCustomerFkConstraint.PrincipalTable); Assert.NotNull(specialCustomerFkConstraint.MappedForeignKeys.Single()); var specialCustomerDbIndex = specialCustomerTable.Indexes.Single(); Assert.Equal("IX_SpecialCustomer_RelatedCustomerSpeciality", specialCustomerDbIndex.Name); Assert.NotNull(specialCustomerDbIndex.MappedIndexes.Single()); Assert.Null(customerType.GetDiscriminatorProperty()); Assert.Null(customerType.GetDiscriminatorValue()); Assert.Null(specialCustomerType.GetDiscriminatorProperty()); Assert.Null(specialCustomerType.GetDiscriminatorValue()); } else { var specialCustomerTypeMapping = specialCustomerType.GetTableMappings().Single(); Assert.True(specialCustomerTypeMapping.IsMainTableMapping); var specialCustomerTable = specialCustomerTypeMapping.Table; Assert.Same(customerTable, specialCustomerTable); Assert.Equal(2, specialCustomerTable.EntityTypeMappings.Count()); Assert.True(specialCustomerTable.EntityTypeMappings.First().IsMainEntityTypeMapping); Assert.False(specialCustomerTable.EntityTypeMappings.Last().IsMainEntityTypeMapping); var specialityColumn = specialCustomerTable.Columns.Single(c => c.Name == nameof(SpecialCustomer.Speciality)); Assert.True(specialityColumn.IsNullable); var specialCustomerPkConstraint = specialCustomerTable.PrimaryKey; Assert.Equal("PK_Customer", specialCustomerPkConstraint.Name); Assert.Same(specialCustomerPkConstraint.MappedKeys.Single(), customerPk); var idProperty = customerPk.Properties.Single(); Assert.Equal(2, idProperty.GetTableColumnMappings().Count()); var specialCustomerUniqueConstraint = specialCustomerTable.UniqueConstraints.Single(c => !c.IsPrimaryKey); Assert.Equal("AK_Customer_SpecialityAk", specialCustomerUniqueConstraint.Name); Assert.NotNull(specialCustomerUniqueConstraint.MappedKeys.Single()); var specialCustomerFkConstraint = specialCustomerTable.ForeignKeyConstraints.Single(); Assert.Equal("FK_Customer_Customer_RelatedCustomerSpeciality", specialCustomerFkConstraint.Name); Assert.NotNull(specialCustomerFkConstraint.MappedForeignKeys.Single()); var specialCustomerDbIndex = specialCustomerTable.Indexes.Single(); Assert.Equal("IX_Customer_RelatedCustomerSpeciality", specialCustomerDbIndex.Name); Assert.NotNull(specialCustomerDbIndex.MappedIndexes.Single()); } }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public override IEnumerable <IAnnotation> For(IRelationalModel model) { return(base.For(model)); }
protected override IEnumerable <MigrationOperation> Diff(IRelationalModel source, IRelationalModel target, DiffContext diffContext) => PostFilterOperations(base.Diff(source, target, diffContext));
protected override IEnumerable <MigrationOperation> Add(IRelationalModel target, DiffContext diffContext) => PostFilterOperations(base.Add(target, diffContext));
private static void AssertTables(IRelationalModel model, Mapping mapping) { var orderType = model.Model.FindEntityType(typeof(Order)); var orderMapping = orderType.GetTableMappings().Single(); Assert.True(orderMapping.IncludesDerivedTypes); Assert.Equal( new[] { nameof(Order.Id), nameof(Order.AlternateId), nameof(Order.CustomerId), nameof(Order.OrderDate) }, orderMapping.ColumnMappings.Select(m => m.Property.Name)); var ordersTable = orderMapping.Table; Assert.Same(ordersTable, model.FindTable(ordersTable.Name, ordersTable.Schema)); Assert.Equal( new[] { nameof(Order), "OrderDetails.BillingAddress#Address", "OrderDetails.ShippingAddress#Address", nameof(OrderDetails) }, ordersTable.EntityTypeMappings.Select(m => m.EntityType.DisplayName())); Assert.Equal( new[] { nameof(Order.Id), nameof(Order.AlternateId), nameof(Order.CustomerId), "Details_BillingAddress_City", "Details_BillingAddress_Street", "Details_ShippingAddress_City", "Details_ShippingAddress_Street", nameof(Order.OrderDate) }, ordersTable.Columns.Select(m => m.Name)); Assert.Equal("Order", ordersTable.Name); Assert.Null(ordersTable.Schema); Assert.False(ordersTable.IsExcludedFromMigrations); Assert.True(ordersTable.IsShared); var orderDate = orderType.FindProperty(nameof(Order.OrderDate)); var orderDateMapping = orderDate.GetTableColumnMappings().Single(); Assert.NotNull(orderDateMapping.TypeMapping); Assert.Equal("default_datetime_mapping", orderDateMapping.TypeMapping.StoreType); Assert.Same(orderMapping, orderDateMapping.TableMapping); var orderDateColumn = orderDateMapping.Column; Assert.Same(orderDateColumn, ordersTable.FindColumn("OrderDate")); Assert.Same(orderDateColumn, orderDate.FindColumn(StoreObjectIdentifier.Table(ordersTable.Name, ordersTable.Schema))); Assert.Same(orderDateColumn, ordersTable.FindColumn(orderDate)); Assert.Equal("OrderDate", orderDateColumn.Name); Assert.Equal("default_datetime_mapping", orderDateColumn.StoreType); Assert.False(orderDateColumn.IsNullable); Assert.Same(ordersTable, orderDateColumn.Table); var orderPk = orderType.FindPrimaryKey(); var orderPkConstraint = orderPk.GetMappedConstraints().Single(); Assert.Equal("PK_Order", orderPkConstraint.Name); Assert.Equal(nameof(Order.Id), orderPkConstraint.Columns.Single().Name); Assert.Same(ordersTable, orderPkConstraint.Table); Assert.True(orderPkConstraint.GetIsPrimaryKey()); Assert.Same(orderPkConstraint, ordersTable.UniqueConstraints.Last()); Assert.Same(orderPkConstraint, ordersTable.PrimaryKey); var orderAk = orderType.GetKeys().Single(k => k != orderPk); var orderAkConstraint = orderAk.GetMappedConstraints().Single(); Assert.Equal("AK_AlternateId", orderAkConstraint.Name); Assert.Equal(nameof(Order.AlternateId), orderAkConstraint.Columns.Single().Name); Assert.Same(ordersTable, orderAkConstraint.Table); Assert.False(orderAkConstraint.GetIsPrimaryKey()); Assert.Same(orderAkConstraint, ordersTable.UniqueConstraints.First()); var orderDateIndex = orderType.GetIndexes().Single(i => i.Properties.Any(p => p.Name == nameof(Order.OrderDate))); var orderDateTableIndex = orderDateIndex.GetMappedTableIndexes().Single(); Assert.Equal("IX_OrderDate", orderDateTableIndex.Name); Assert.Equal(nameof(Order.OrderDate), orderDateTableIndex.Columns.Single().Name); Assert.Same(ordersTable, orderDateTableIndex.Table); Assert.True(orderDateTableIndex.IsUnique); Assert.Null(orderDateTableIndex.Filter); Assert.Equal(orderDateTableIndex, ordersTable.Indexes.Last()); var orderCustomerIndex = orderType.GetIndexes().Single(i => i.Properties.Any(p => p.Name == nameof(Order.CustomerId))); var orderTableIndex = orderCustomerIndex.GetMappedTableIndexes().Single(); Assert.Equal("IX_Order_CustomerId", orderTableIndex.Name); Assert.Equal(nameof(Order.CustomerId), orderTableIndex.Columns.Single().Name); Assert.Same(ordersTable, orderTableIndex.Table); Assert.False(orderTableIndex.IsUnique); Assert.Null(orderTableIndex.Filter); Assert.Equal(orderCustomerIndex, orderTableIndex.MappedIndexes.Single()); Assert.Same(orderTableIndex, ordersTable.Indexes.First()); var orderDateFk = orderType.GetForeignKeys().Single(fk => fk.PrincipalEntityType.ClrType == typeof(DateDetails)); var orderDateFkConstraint = orderDateFk.GetMappedConstraints().Single(); Assert.Equal("FK_DateDetails", orderDateFkConstraint.Name); Assert.Equal(nameof(Order.OrderDate), orderDateFkConstraint.Columns.Single().Name); Assert.Equal(nameof(DateDetails.Date), orderDateFkConstraint.PrincipalColumns.Single().Name); Assert.Equal("DateDetails", orderDateFkConstraint.PrincipalTable.Name); var orderCustomerFk = orderType.GetForeignKeys().Single(fk => fk.PrincipalEntityType.ClrType == typeof(Customer)); var orderCustomerFkConstraint = orderCustomerFk.GetMappedConstraints().Single(); Assert.Equal("FK_Order_Customer_CustomerId", orderCustomerFkConstraint.Name); Assert.Equal(nameof(Order.CustomerId), orderCustomerFkConstraint.Columns.Single().Name); Assert.Equal(nameof(Customer.Id), orderCustomerFkConstraint.PrincipalColumns.Single().Name); Assert.Same(ordersTable, orderCustomerFkConstraint.Table); Assert.Equal("Customer", orderCustomerFkConstraint.PrincipalTable.Name); Assert.Equal(ReferentialAction.Cascade, orderCustomerFkConstraint.OnDeleteAction); Assert.Equal(orderCustomerFk, orderCustomerFkConstraint.MappedForeignKeys.Single()); Assert.Equal(new[] { orderDateFkConstraint, orderCustomerFkConstraint }, ordersTable.ForeignKeyConstraints); var specialCustomerType = model.Model.FindEntityType(typeof(SpecialCustomer)); var extraSpecialCustomerType = model.Model.FindEntityType(typeof(ExtraSpecialCustomer)); var orderDetailsOwnership = orderType.FindNavigation(nameof(Order.Details)).ForeignKey; var orderDetailsType = orderDetailsOwnership.DeclaringEntityType; Assert.Same(ordersTable, orderDetailsType.GetTableMappings().Single().Table); Assert.Equal( ordersTable.GetReferencingRowInternalForeignKeys(orderType), ordersTable.GetRowInternalForeignKeys(orderDetailsType)); Assert.Equal( RelationalStrings.TableNotMappedEntityType(nameof(SpecialCustomer), ordersTable.Name), Assert.Throws <InvalidOperationException>( () => ordersTable.GetReferencingRowInternalForeignKeys(specialCustomerType)).Message); Assert.Equal( RelationalStrings.TableNotMappedEntityType(nameof(SpecialCustomer), ordersTable.Name), Assert.Throws <InvalidOperationException>( () => ordersTable.GetRowInternalForeignKeys(specialCustomerType)).Message); Assert.False(ordersTable.IsOptional(orderType)); Assert.True(ordersTable.IsOptional(orderDetailsType)); Assert.Equal( RelationalStrings.TableNotMappedEntityType(nameof(SpecialCustomer), ordersTable.Name), Assert.Throws <InvalidOperationException>( () => ordersTable.IsOptional(specialCustomerType)).Message); Assert.Empty(orderDetailsOwnership.GetMappedConstraints()); Assert.Equal(2, orderDetailsType.GetForeignKeys().Count()); var orderDetailsDateIndex = orderDetailsType.GetIndexes().Single(i => i.Properties.Any(p => p.Name == nameof(Order.OrderDate))); var orderDetailsDateTableIndex = orderDetailsDateIndex.GetMappedTableIndexes().Single(); Assert.Same(orderDateTableIndex, orderDetailsDateTableIndex); Assert.Equal(new[] { orderDateIndex, orderDetailsDateIndex }, orderDateTableIndex.MappedIndexes); var orderDetailsPk = orderDetailsType.FindPrimaryKey(); Assert.Same(orderPkConstraint, orderDetailsPk.GetMappedConstraints().Single()); var orderDetailsPkProperty = orderDetailsPk.Properties.Single(); #pragma warning disable CS0618 // Type or member is obsolete Assert.Equal("Id", orderDetailsPkProperty.GetColumnName()); #pragma warning restore CS0618 // Type or member is obsolete Assert.Equal("OrderId", orderDetailsPkProperty.GetColumnBaseName()); var billingAddressOwnership = orderDetailsType.FindNavigation(nameof(OrderDetails.BillingAddress)).ForeignKey; var billingAddressType = billingAddressOwnership.DeclaringEntityType; var shippingAddressOwnership = orderDetailsType.FindNavigation(nameof(OrderDetails.ShippingAddress)).ForeignKey; var shippingAddressType = shippingAddressOwnership.DeclaringEntityType; Assert.Equal( new[] { orderPk, billingAddressType.FindPrimaryKey(), shippingAddressType.FindPrimaryKey(), orderDetailsPk }, orderPkConstraint.MappedKeys); var orderDetailsDate = orderDetailsType.FindProperty(nameof(OrderDetails.OrderDate)); Assert.Equal(new[] { orderDate, orderDetailsDate }, orderDateColumn.PropertyMappings.Select(m => m.Property)); var orderDetailsAk = orderDetailsType.GetKeys().Single(k => k != orderDetailsPk); var orderDetailsAkConstraint = orderDetailsAk.GetMappedConstraints().Single(); Assert.Same(orderAkConstraint, orderDetailsAkConstraint); Assert.Equal(new[] { orderAk, orderDetailsAk }, orderAkConstraint.MappedKeys); var orderDetailsDateFk = orderDetailsType.GetForeignKeys().Single(fk => fk.PrincipalEntityType.ClrType == typeof(DateDetails)); var orderDetailsDateFkConstraint = orderDateFk.GetMappedConstraints().Single(); Assert.Same(orderDateFkConstraint, orderDetailsDateFkConstraint); Assert.Equal(new[] { orderDateFk, orderDetailsDateFk }, orderDateFkConstraint.MappedForeignKeys); Assert.Equal("FK_DateDetails", orderDateFkConstraint.Name); var customerType = model.Model.FindEntityType(typeof(Customer)); var customerTable = customerType.GetTableMappings().Single().Table; Assert.Equal("Customer", customerTable.Name); Assert.False(customerTable.IsOptional(customerType)); Assert.False(customerTable.IsOptional(specialCustomerType)); Assert.False(customerTable.IsOptional(extraSpecialCustomerType)); var customerPk = specialCustomerType.FindPrimaryKey(); if (mapping == Mapping.TPT) { Assert.Equal(2, specialCustomerType.GetTableMappings().Count()); Assert.True(specialCustomerType.GetTableMappings().First().IsSplitEntityTypePrincipal); Assert.False(specialCustomerType.GetTableMappings().First().IncludesDerivedTypes); Assert.True(specialCustomerType.GetTableMappings().Last().IsSplitEntityTypePrincipal); Assert.True(specialCustomerType.GetTableMappings().Last().IncludesDerivedTypes); var specialCustomerTable = specialCustomerType.GetTableMappings().Select(t => t.Table).First(t => t.Name == "SpecialCustomer"); Assert.Equal("SpecialSchema", specialCustomerTable.Schema); Assert.Equal(4, specialCustomerTable.Columns.Count()); Assert.True(specialCustomerTable.EntityTypeMappings.Single(m => m.EntityType == specialCustomerType).IsSharedTablePrincipal); var specialityColumn = specialCustomerTable.Columns.Single(c => c.Name == nameof(SpecialCustomer.Speciality)); Assert.False(specialityColumn.IsNullable); var specialityProperty = specialityColumn.PropertyMappings.First().Property; Assert.Equal( RelationalStrings.PropertyNotMappedToTable( nameof(SpecialCustomer.Speciality), nameof(SpecialCustomer), "Customer"), Assert.Throws <InvalidOperationException>(() => specialityProperty.IsColumnNullable(StoreObjectIdentifier.Table(customerTable.Name, customerTable.Schema))).Message); Assert.Equal(3, customerPk.GetMappedConstraints().Count()); var specialCustomerPkConstraint = specialCustomerTable.PrimaryKey; Assert.Equal("PK_SpecialCustomer", specialCustomerPkConstraint.Name); Assert.Same(specialCustomerPkConstraint.MappedKeys.Single(), customerPk); var idProperty = customerPk.Properties.Single(); Assert.Equal(6, idProperty.GetTableColumnMappings().Count()); Assert.Empty(customerTable.ForeignKeyConstraints); var specialCustomerUniqueConstraint = customerTable.UniqueConstraints.Single(c => !c.GetIsPrimaryKey()); Assert.Equal("AK_Customer_SpecialityAk", specialCustomerUniqueConstraint.Name); Assert.NotNull(specialCustomerUniqueConstraint.MappedKeys.Single()); var foreignKeys = specialCustomerTable.ForeignKeyConstraints.ToArray(); Assert.Equal(3, foreignKeys.Length); var specialCustomerTptFkConstraint = foreignKeys[0]; Assert.Equal("FK_SpecialCustomer_Customer_Id", specialCustomerTptFkConstraint.Name); Assert.NotNull(specialCustomerTptFkConstraint.MappedForeignKeys.Single()); Assert.Same(customerTable, specialCustomerTptFkConstraint.PrincipalTable); var specialCustomerFkConstraint = foreignKeys[1]; Assert.Equal("FK_SpecialCustomer_Customer_RelatedCustomerSpeciality", specialCustomerFkConstraint.Name); Assert.NotNull(specialCustomerFkConstraint.MappedForeignKeys.Single()); Assert.Same(customerTable, specialCustomerFkConstraint.PrincipalTable); var anotherSpecialCustomerFkConstraint = foreignKeys[2]; Assert.Equal("FK_SpecialCustomer_SpecialCustomer_AnotherRelatedCustomerId", anotherSpecialCustomerFkConstraint.Name); Assert.NotNull(anotherSpecialCustomerFkConstraint.MappedForeignKeys.Single()); Assert.Same(specialCustomerTable, anotherSpecialCustomerFkConstraint.PrincipalTable); var specialCustomerDbIndex = specialCustomerTable.Indexes.Last(); Assert.Equal("IX_SpecialCustomer_RelatedCustomerSpeciality", specialCustomerDbIndex.Name); Assert.NotNull(specialCustomerDbIndex.MappedIndexes.Single()); var anotherSpecialCustomerDbIndex = specialCustomerTable.Indexes.First(); Assert.Equal("IX_SpecialCustomer_AnotherRelatedCustomerId", anotherSpecialCustomerDbIndex.Name); Assert.NotNull(anotherSpecialCustomerDbIndex.MappedIndexes.Single()); Assert.Null(customerType.GetDiscriminatorProperty()); Assert.Null(customerType.GetDiscriminatorValue()); Assert.Null(specialCustomerType.GetDiscriminatorProperty()); Assert.Null(specialCustomerType.GetDiscriminatorValue()); } else { var specialCustomerTypeMapping = specialCustomerType.GetTableMappings().Single(); Assert.True(specialCustomerTypeMapping.IsSplitEntityTypePrincipal); Assert.True(specialCustomerTypeMapping.IncludesDerivedTypes); var specialCustomerTable = specialCustomerTypeMapping.Table; Assert.Same(customerTable, specialCustomerTable); Assert.Equal(3, specialCustomerTable.EntityTypeMappings.Count()); Assert.True(specialCustomerTable.EntityTypeMappings.First().IsSharedTablePrincipal); Assert.False(specialCustomerTable.EntityTypeMappings.Last().IsSharedTablePrincipal); var specialityColumn = specialCustomerTable.Columns.Single(c => c.Name == nameof(SpecialCustomer.Speciality)); Assert.True(specialityColumn.IsNullable); var specialCustomerPkConstraint = specialCustomerTable.PrimaryKey; Assert.Equal("PK_Customer", specialCustomerPkConstraint.Name); Assert.Same(specialCustomerPkConstraint.MappedKeys.Single(), customerPk); var idProperty = customerPk.Properties.Single(); Assert.Equal(3, idProperty.GetTableColumnMappings().Count()); var specialCustomerUniqueConstraint = specialCustomerTable.UniqueConstraints.Single(c => !c.GetIsPrimaryKey()); Assert.Equal("AK_Customer_SpecialityAk", specialCustomerUniqueConstraint.Name); Assert.NotNull(specialCustomerUniqueConstraint.MappedKeys.Single()); var specialCustomerFkConstraint = specialCustomerTable.ForeignKeyConstraints.Last(); Assert.Equal("FK_Customer_Customer_RelatedCustomerSpeciality", specialCustomerFkConstraint.Name); Assert.NotNull(specialCustomerFkConstraint.MappedForeignKeys.Single()); var anotherSpecialCustomerFkConstraint = specialCustomerTable.ForeignKeyConstraints.First(); Assert.Equal("FK_Customer_Customer_AnotherRelatedCustomerId", anotherSpecialCustomerFkConstraint.Name); Assert.NotNull(anotherSpecialCustomerFkConstraint.MappedForeignKeys.Single()); var specialCustomerDbIndex = specialCustomerTable.Indexes.Last(); Assert.Equal("IX_Customer_RelatedCustomerSpeciality", specialCustomerDbIndex.Name); Assert.NotNull(specialCustomerDbIndex.MappedIndexes.Single()); var anotherSpecialCustomerDbIndex = specialCustomerTable.Indexes.First(); Assert.Equal("IX_Customer_AnotherRelatedCustomerId", anotherSpecialCustomerDbIndex.Name); Assert.NotNull(specialCustomerDbIndex.MappedIndexes.Single()); } }
public override IEnumerable <IAnnotation> For(IRelationalModel model) => model.Model.GetAnnotations().Where(a => a.Name.StartsWith(NpgsqlAnnotationNames.PostgresExtensionPrefix, StringComparison.Ordinal) || a.Name.StartsWith(NpgsqlAnnotationNames.EnumPrefix, StringComparison.Ordinal) || a.Name.StartsWith(NpgsqlAnnotationNames.RangePrefix, StringComparison.Ordinal) || a.Name.StartsWith(NpgsqlAnnotationNames.CollationDefinitionPrefix, StringComparison.Ordinal));
/// <inheritdoc /> public virtual IEnumerable <IAnnotation> For(IRelationalModel model, bool designTime) => Enumerable.Empty <IAnnotation>();
public override IReadOnlyList <MigrationOperation> GetDifferences(IRelationalModel source, IRelationalModel target) { var deleteTriggerOperations = new List <SqlOperation>(); var createTriggerOperations = new List <SqlOperation>(); var sourceModel = source?.Model; var targetModel = target?.Model; var oldEntityTypeNames = sourceModel?.GetEntityTypes().Select(x => x.Name) ?? Enumerable.Empty <string>(); var newEntityTypeNames = targetModel?.GetEntityTypes().Select(x => x.Name) ?? Enumerable.Empty <string>(); var commonEntityTypeNames = oldEntityTypeNames.Intersect(newEntityTypeNames); // Drop all triggers for deleted entities. foreach (var deletedTypeName in oldEntityTypeNames.Except(commonEntityTypeNames)) { var deletedEntityType = source.Model.FindEntityType(deletedTypeName); foreach (var annotation in deletedEntityType.GetTriggerAnnotations()) { deleteTriggerOperations.AddDeleteTriggerSqlMigration(annotation, sourceModel); } } // Add all triggers to created entities. foreach (var newTypeName in newEntityTypeNames.Except(commonEntityTypeNames)) { foreach (var annotation in targetModel.FindEntityType(newTypeName).GetTriggerAnnotations()) { createTriggerOperations.AddCreateTriggerSqlMigration(annotation); } } // For existing entities. foreach (var entityTypeName in commonEntityTypeNames) { var oldEntityType = sourceModel.FindEntityType(entityTypeName); var newEntityType = targetModel.FindEntityType(entityTypeName); var oldAnnotationNames = sourceModel.FindEntityType(entityTypeName) .GetTriggerAnnotations() .Select(x => x.Name); var newAnnotationNames = targetModel.FindEntityType(entityTypeName) .GetTriggerAnnotations() .Select(x => x.Name); var commonAnnotationNames = oldAnnotationNames.Intersect(newAnnotationNames); // If trigger was changed, recreate it. foreach (var commonAnnotationName in commonAnnotationNames) { var oldValue = sourceModel.FindEntityType(entityTypeName).GetAnnotation(commonAnnotationName); var newValue = targetModel.FindEntityType(entityTypeName).GetAnnotation(commonAnnotationName); if ((string)oldValue.Value != (string)newValue.Value) { deleteTriggerOperations.AddDeleteTriggerSqlMigration(oldValue, sourceModel); createTriggerOperations.AddCreateTriggerSqlMigration(newValue); } } // If trigger was removed, delete it. foreach (var oldTriggerName in oldAnnotationNames.Except(commonAnnotationNames)) { var oldTriggerAnnotation = oldEntityType.GetAnnotation(oldTriggerName); deleteTriggerOperations.AddDeleteTriggerSqlMigration(oldTriggerAnnotation, sourceModel); } // If trigger was added, create it. foreach (var newTriggerName in newAnnotationNames.Except(commonAnnotationNames)) { var newTriggerAnnotation = newEntityType.GetAnnotation(newTriggerName); createTriggerOperations.AddCreateTriggerSqlMigration(newTriggerAnnotation); } } return(MergeOperations(base.GetDifferences(source, target), createTriggerOperations, deleteTriggerOperations)); }
/// <inheritdoc /> public virtual IEnumerable <IAnnotation> ForRemove(IRelationalModel model) => Enumerable.Empty <IAnnotation>();
public IEnumerable <IAnnotation> ForRemove(IRelationalModel model) => _providers.SelectMany(p => p.ForRemove(model));
private static void AssertViews(IRelationalModel model, Mapping mapping) { var orderType = model.Model.FindEntityType(typeof(Order)); var orderMapping = orderType.GetViewMappings().Single(); Assert.Same(orderType.GetViewMappings(), orderType.GetViewOrTableMappings()); Assert.True(orderMapping.IncludesDerivedTypes); Assert.Equal( new[] { nameof(Order.AlternateId), nameof(Order.CustomerId), nameof(Order.OrderDate), nameof(Order.OrderId) }, orderMapping.ColumnMappings.Select(m => m.Property.Name)); var ordersView = orderMapping.View; Assert.Same(ordersView, model.FindView(ordersView.Name, ordersView.Schema)); Assert.Equal( new[] { "OrderDetails.BillingAddress#Address", "OrderDetails.ShippingAddress#Address", nameof(Order), nameof(OrderDetails) }, ordersView.EntityTypeMappings.Select(m => m.EntityType.DisplayName())); Assert.Equal(new[] { nameof(Order.AlternateId), nameof(Order.CustomerId), "Details_BillingAddress_City", "Details_BillingAddress_Street", "Details_ShippingAddress_City", "Details_ShippingAddress_Street", "OrderDateView", nameof(Order.OrderId) }, ordersView.Columns.Select(m => m.Name)); Assert.Equal("OrderView", ordersView.Name); Assert.Equal("viewSchema", ordersView.Schema); Assert.Null(ordersView.ViewDefinitionSql); var orderDate = orderType.FindProperty(nameof(Order.OrderDate)); Assert.False(orderDate.IsViewColumnNullable()); var orderDateMapping = orderDate.GetViewColumnMappings().Single(); Assert.NotNull(orderDateMapping.TypeMapping); Assert.Equal("default_datetime_mapping", orderDateMapping.TypeMapping.StoreType); Assert.Same(orderMapping, orderDateMapping.ViewMapping); var orderDetailsOwnership = orderType.FindNavigation(nameof(Order.Details)).ForeignKey; var orderDetailsType = orderDetailsOwnership.DeclaringEntityType; Assert.Same(ordersView, orderDetailsType.GetViewMappings().Single().View); Assert.Equal(ordersView.GetReferencingRowInternalForeignKeys(orderType), ordersView.GetRowInternalForeignKeys(orderDetailsType)); var orderDetailsDate = orderDetailsType.FindProperty(nameof(OrderDetails.OrderDate)); Assert.True(orderDetailsDate.IsViewColumnNullable()); var orderDateColumn = orderDateMapping.Column; Assert.Same(orderDateColumn, ordersView.FindColumn("OrderDateView")); Assert.Same(orderDateColumn, orderDate.FindViewColumn(ordersView.Name, ordersView.Schema)); Assert.Equal(new[] { orderDate, orderDetailsDate }, orderDateColumn.PropertyMappings.Select(m => m.Property)); Assert.Equal("OrderDateView", orderDateColumn.Name); Assert.Equal("default_datetime_mapping", orderDateColumn.StoreType); Assert.False(orderDateColumn.IsNullable); Assert.Same(ordersView, orderDateColumn.Table); var customerType = model.Model.FindEntityType(typeof(Customer)); var customerView = customerType.GetViewMappings().Single().View; Assert.Equal("CustomerView", customerView.Name); Assert.Equal("viewSchema", customerView.Schema); var specialCustomerType = model.Model.FindEntityType(typeof(SpecialCustomer)); var customerPk = specialCustomerType.FindPrimaryKey(); if (mapping == Mapping.TPT) { Assert.Equal(2, specialCustomerType.GetViewMappings().Count()); Assert.True(specialCustomerType.GetViewMappings().First().IsMainTableMapping); Assert.False(specialCustomerType.GetViewMappings().Last().IsMainTableMapping); var specialCustomerView = specialCustomerType.GetViewMappings().Select(t => t.Table).First(t => t.Name == "SpecialCustomerView"); Assert.Null(specialCustomerView.Schema); Assert.Equal(3, specialCustomerView.Columns.Count()); Assert.True(specialCustomerView.EntityTypeMappings.Single().IsMainEntityTypeMapping); var specialityColumn = specialCustomerView.Columns.Single(c => c.Name == nameof(SpecialCustomer.Speciality)); Assert.False(specialityColumn.IsNullable); Assert.Null(customerType.GetDiscriminatorProperty()); Assert.Null(customerType.GetDiscriminatorValue()); Assert.Null(specialCustomerType.GetDiscriminatorProperty()); Assert.Null(specialCustomerType.GetDiscriminatorValue()); } else { var specialCustomerViewMapping = specialCustomerType.GetViewMappings().Single(); Assert.True(specialCustomerViewMapping.IsMainTableMapping); var specialCustomerView = specialCustomerViewMapping.View; Assert.Same(customerView, specialCustomerView); Assert.Equal(2, specialCustomerView.EntityTypeMappings.Count()); Assert.True(specialCustomerView.EntityTypeMappings.First().IsMainEntityTypeMapping); Assert.False(specialCustomerView.EntityTypeMappings.Last().IsMainEntityTypeMapping); var specialityColumn = specialCustomerView.Columns.Single(c => c.Name == nameof(SpecialCustomer.Speciality)); Assert.True(specialityColumn.IsNullable); } }
private static void AssertTables(IRelationalModel model) { var orderType = model.Model.FindEntityType(typeof(Order)); var orderMapping = orderType.GetTableMappings().Single(); Assert.True(orderMapping.IncludesDerivedTypes); Assert.Equal( new[] { nameof(Order.CustomerId), nameof(Order.OrderDate), nameof(Order.OrderId) }, orderMapping.ColumnMappings.Select(m => m.Property.Name)); var ordersTable = orderMapping.Table; Assert.Same(ordersTable, model.FindTable(ordersTable.Name, ordersTable.Schema)); Assert.Equal( new[] { "OrderDetails.BillingAddress#Address", "OrderDetails.ShippingAddress#Address", nameof(Order), nameof(OrderDetails) }, ordersTable.EntityTypeMappings.Select(m => m.EntityType.DisplayName())); Assert.Equal(new[] { nameof(Order.CustomerId), "Details_BillingAddress_City", "Details_BillingAddress_Street", "Details_ShippingAddress_City", "Details_ShippingAddress_Street", nameof(Order.OrderDate), nameof(Order.OrderId) }, ordersTable.Columns.Select(m => m.Name)); Assert.Equal("Order", ordersTable.Name); Assert.Null(ordersTable.Schema); Assert.True(ordersTable.IsMigratable); Assert.True(ordersTable.IsSplit); var orderDate = orderType.FindProperty(nameof(Order.OrderDate)); Assert.False(orderDate.IsColumnNullable()); var orderDateMapping = orderDate.GetTableColumnMappings().Single(); Assert.NotNull(orderDateMapping.TypeMapping); Assert.Equal("default_datetime_mapping", orderDateMapping.TypeMapping.StoreType); Assert.Same(orderMapping, orderDateMapping.TableMapping); var orderPk = orderType.GetKeys().Single(); var orderPkConstraint = orderPk.GetMappedConstraints().Single(); Assert.Equal("PK_Order", orderPkConstraint.Name); Assert.Equal(nameof(Order.OrderId), orderPkConstraint.Columns.Single().Name); Assert.Same(ordersTable, orderPkConstraint.Table); Assert.True(orderPkConstraint.IsPrimaryKey); Assert.Contains(orderPk, orderPkConstraint.MappedKeys); Assert.Same(orderPkConstraint, ordersTable.UniqueConstraints.Single()); Assert.Same(orderPkConstraint, ordersTable.PrimaryKey); var orderIndex = orderType.GetIndexes().Single(); var orderTableIndex = orderIndex.GetMappedTableIndexes().Single(); Assert.Equal("IX_Order_CustomerId", orderTableIndex.Name); Assert.Equal(nameof(Order.CustomerId), orderTableIndex.Columns.Single().Name); Assert.Same(ordersTable, orderTableIndex.Table); Assert.False(orderTableIndex.IsUnique); Assert.Null(orderTableIndex.Filter); Assert.Equal(orderIndex, orderTableIndex.MappedIndexes.Single()); Assert.Same(orderTableIndex, ordersTable.Indexes.Single()); var orderFk = orderType.GetForeignKeys().Single(); var orderFkConstraint = orderFk.GetMappedConstraints().Single(); Assert.Equal("FK_Order_Customer_CustomerId", orderFkConstraint.Name); Assert.Equal(nameof(Order.CustomerId), orderFkConstraint.Columns.Single().Name); Assert.Equal(nameof(Customer.Id), orderFkConstraint.PrincipalColumns.Single().Name); Assert.Same(ordersTable, orderFkConstraint.Table); Assert.Equal("Customer", orderFkConstraint.PrincipalTable.Name); Assert.Equal(ReferentialAction.Cascade, orderFkConstraint.OnDeleteAction); Assert.Equal(orderFk, orderFkConstraint.MappedForeignKeys.Single()); Assert.Same(orderFkConstraint, ordersTable.ForeignKeyConstraints.Single()); var orderDetailsOwnership = orderType.FindNavigation(nameof(Order.Details)).ForeignKey; var orderDetailsType = orderDetailsOwnership.DeclaringEntityType; Assert.Same(ordersTable, orderDetailsType.GetTableMappings().Single().Table); Assert.Equal(ordersTable.GetReferencingInternalForeignKeys(orderType), ordersTable.GetInternalForeignKeys(orderDetailsType)); Assert.Empty(orderDetailsOwnership.GetMappedConstraints()); Assert.Empty(orderDetailsType.GetForeignKeys().Where(fk => fk != orderDetailsOwnership)); Assert.Same(orderPkConstraint, orderDetailsType.FindPrimaryKey().GetMappedConstraints().Single()); Assert.Contains(orderDetailsType.FindPrimaryKey(), orderPkConstraint.MappedKeys); var orderDetailsDate = orderDetailsType.FindProperty(nameof(OrderDetails.OrderDate)); Assert.True(orderDetailsDate.IsColumnNullable()); var orderDateColumn = orderDateMapping.Column; Assert.Same(orderDateColumn, ordersTable.FindColumn("OrderDate")); Assert.Same(orderDateColumn, orderDate.FindTableColumn(ordersTable.Name, ordersTable.Schema)); Assert.Equal(new[] { orderDate, orderDetailsDate }, orderDateColumn.PropertyMappings.Select(m => m.Property)); Assert.Equal("OrderDate", orderDateColumn.Name); Assert.Equal("default_datetime_mapping", orderDateColumn.StoreType); Assert.False(orderDateColumn.IsNullable); Assert.Same(ordersTable, orderDateColumn.Table); var customerType = model.Model.FindEntityType(typeof(Customer)); var customerTable = customerType.GetTableMappings().Single().Table; Assert.Equal("Customer", customerTable.Name); Assert.Empty(customerTable.ForeignKeyConstraints); var specialCustomerType = model.Model.FindEntityType(typeof(SpecialCustomer)); Assert.Same(customerTable, specialCustomerType.GetTableMappings().Single().Table); }