Skip to content

Commit

Permalink
Undo GetColumnName behavior change
Browse files Browse the repository at this point in the history
Fixes #22608
  • Loading branch information
AndriySvyryd committed Sep 22, 2020
1 parent 3a1fd2d commit f7805c8
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ private void GenerateKeyAttribute(IProperty property)

private void GenerateColumnAttribute(IProperty property)
{
var columnName = property.GetColumnName();
var columnName = property.GetColumnBaseName();
var columnType = property.GetConfiguredColumnType();

var delimitedColumnName = columnName != null && columnName != property.Name ? _code.Literal(columnName) : null;
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Design/AnnotationCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public virtual void RemoveAnnotationsHandledByConventions(
IProperty property,
IDictionary<string, IAnnotation> annotations)
{
var columnName = property.GetColumnName();
var columnName = property.GetColumnBaseName();
if (columnName == property.Name)
{
annotations.Remove(RelationalAnnotationNames.ColumnName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static string GetDefaultName([NotNull] this IForeignKey foreignKey)
.Append("_")
.Append(principalTableName)
.Append("_")
.AppendJoin(foreignKey.Properties.Select(p => p.GetColumnName()), "_")
.AppendJoin(foreignKey.Properties.Select(p => p.GetColumnBaseName()), "_")
.ToString();

return Uniquifier.Truncate(name, foreignKey.DeclaringEntityType.Model.GetMaxIdentifierLength());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static string GetDefaultDatabaseName([NotNull] this IIndex index)
.Append("IX_")
.Append(tableName)
.Append("_")
.AppendJoin(index.Properties.Select(p => p.GetColumnName()), "_")
.AppendJoin(index.Properties.Select(p => p.GetColumnBaseName()), "_")
.ToString();

return Uniquifier.Truncate(baseName, index.DeclaringEntityType.Model.GetMaxIdentifierLength());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static string GetDefaultName([NotNull] this IKey key)
.Append("AK_")
.Append(tableName)
.Append("_")
.AppendJoin(key.Properties.Select(p => p.GetColumnName()), "_")
.AppendJoin(key.Properties.Select(p => p.GetColumnBaseName()), "_")
.ToString();
}

Expand Down
32 changes: 27 additions & 5 deletions src/EFCore.Relational/Extensions/RelationalPropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,27 @@ namespace Microsoft.EntityFrameworkCore
public static class RelationalPropertyExtensions
{
/// <summary>
/// Returns the name of the column to which the property is mapped.
/// Returns the name of the table column to which the property is mapped.
/// </summary>
/// <param name="property"> The property. </param>
/// <returns> The name of the column to which the property is mapped. </returns>
/// <returns> The name of the table column to which the property is mapped. </returns>
public static string GetColumnName([NotNull] this IProperty property)
{
var annotation = property.FindAnnotation(RelationalAnnotationNames.ColumnName);
return annotation != null ? (string)annotation.Value : property.GetDefaultColumnName();
}

/// <summary>
/// Returns the base name of the column to which the property would be mapped.
/// </summary>
/// <param name="property"> The property. </param>
/// <returns> The the base name of the column to which the property would be mapped. </returns>
public static string GetColumnBaseName([NotNull] this IProperty property)
{
var annotation = property.FindAnnotation(RelationalAnnotationNames.ColumnName);
return annotation != null ? (string)annotation.Value : property.GetDefaultColumnBaseName();
}

/// <summary>
/// Returns the name of the column to which the property is mapped for a particular table.
/// </summary>
Expand Down Expand Up @@ -86,11 +97,22 @@ public static string GetColumnName([NotNull] this IProperty property, in StoreOb
}

/// <summary>
/// Returns the default column name to which the property would be mapped.
/// Returns the default table column name to which the property would be mapped.
/// </summary>
/// <param name="property"> The property. </param>
/// <returns> The default column name to which the property would be mapped. </returns>
/// <returns> The default table column name to which the property would be mapped. </returns>
public static string GetDefaultColumnName([NotNull] this IProperty property)
{
var table = StoreObjectIdentifier.Create(property.DeclaringEntityType, StoreObjectType.Table);
return table == null ? property.GetDefaultColumnBaseName() : property.GetDefaultColumnName(table.Value);
}

/// <summary>
/// Returns the default base name of the column to which the property would be mapped
/// </summary>
/// <param name="property"> The property. </param>
/// <returns> The default base column name to which the property would be mapped. </returns>
public static string GetDefaultColumnBaseName([NotNull] this IProperty property)
=> Uniquifier.Truncate(property.Name, property.DeclaringEntityType.Model.GetMaxIdentifierLength());

/// <summary>
Expand Down Expand Up @@ -170,7 +192,7 @@ public static string GetDefaultColumnName([NotNull] this IProperty property, in
entityType = ownerType;
}

var baseName = property.GetDefaultColumnName();
var baseName = property.GetDefaultColumnBaseName();
if (builder == null)
{
return baseName;
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Metadata/Internal/RelationalModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private static void AddDefaultMappings(RelationalModel databaseModel, IConventio

foreach (var property in entityType.GetProperties())
{
var columnName = property.GetColumnName();
var columnName = property.GetColumnBaseName();
if (columnName == null)
{
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Migrations/HistoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected virtual string MigrationIdColumnName
=> _migrationIdColumnName ??= EnsureModel()
.FindEntityType(typeof(HistoryRow))
.FindProperty(nameof(HistoryRow.MigrationId))
.GetColumnName();
.GetColumnBaseName();

private IModel EnsureModel()
{
Expand Down Expand Up @@ -119,7 +119,7 @@ protected virtual string ProductVersionColumnName
=> _productVersionColumnName ??= EnsureModel()
.FindEntityType(typeof(HistoryRow))
.FindProperty(nameof(HistoryRow.ProductVersion))
.GetColumnName();
.GetColumnBaseName();

/// <summary>
/// Overridden by database providers to generate SQL that tests for existence of the history table.
Expand Down
32 changes: 18 additions & 14 deletions test/EFCore.Relational.Tests/Metadata/RelationalModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ private static void AssertDefaultMappings(IRelationalModel model)
var orderMapping = orderType.GetDefaultMappings().Single();
Assert.True(orderMapping.IncludesDerivedTypes);
Assert.Equal(
new[] { nameof(Order.OrderId), nameof(Order.AlternateId), nameof(Order.CustomerId), nameof(Order.OrderDate) },
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), "OrderDate", nameof(Order.OrderId) },
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);
Expand Down Expand Up @@ -139,7 +139,7 @@ private static void AssertViews(IRelationalModel model, Mapping mapping)
Assert.Same(orderType.GetViewMappings(), orderType.GetViewOrTableMappings());
Assert.True(orderMapping.IncludesDerivedTypes);
Assert.Equal(
new[] { nameof(Order.OrderId), nameof(Order.AlternateId), nameof(Order.CustomerId), nameof(Order.OrderDate) },
new[] { nameof(Order.Id), nameof(Order.AlternateId), nameof(Order.CustomerId), nameof(Order.OrderDate) },
orderMapping.ColumnMappings.Select(m => m.Property.Name));

var ordersView = orderMapping.View;
Expand All @@ -159,8 +159,8 @@ private static void AssertViews(IRelationalModel model, Mapping mapping)
"Details_BillingAddress_Street",
"Details_ShippingAddress_City",
"Details_ShippingAddress_Street",
"OrderDate",
nameof(Order.OrderId)
nameof(Order.Id),
"OrderDate"
},
ordersView.Columns.Select(m => m.Name));
Assert.Equal("OrderView", ordersView.Name);
Expand Down Expand Up @@ -249,7 +249,7 @@ private static void AssertTables(IRelationalModel model, Mapping mapping)
var orderMapping = orderType.GetTableMappings().Single();
Assert.True(orderMapping.IncludesDerivedTypes);
Assert.Equal(
new[] { nameof(Order.OrderId), nameof(Order.AlternateId), nameof(Order.CustomerId), nameof(Order.OrderDate) },
new[] { nameof(Order.Id), nameof(Order.AlternateId), nameof(Order.CustomerId), nameof(Order.OrderDate) },
orderMapping.ColumnMappings.Select(m => m.Property.Name));

var ordersTable = orderMapping.Table;
Expand All @@ -263,7 +263,7 @@ private static void AssertTables(IRelationalModel model, Mapping mapping)
Assert.Equal(
new[]
{
nameof(Order.OrderId),
nameof(Order.Id),
nameof(Order.AlternateId),
nameof(Order.CustomerId),
"Details_BillingAddress_City",
Expand Down Expand Up @@ -298,7 +298,7 @@ private static void AssertTables(IRelationalModel model, Mapping mapping)
var orderPkConstraint = orderPk.GetMappedConstraints().Single();

Assert.Equal("PK_Order", orderPkConstraint.Name);
Assert.Equal(nameof(Order.OrderId), orderPkConstraint.Columns.Single().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());
Expand Down Expand Up @@ -388,14 +388,18 @@ private static void AssertTables(IRelationalModel model, Mapping mapping)
var orderDetailsPk = orderDetailsType.FindPrimaryKey();
Assert.Same(orderPkConstraint, orderDetailsPk.GetMappedConstraints().Single());

var orderDetailsPkProperty = orderDetailsPk.Properties.Single();
Assert.Equal("Id", orderDetailsPkProperty.GetColumnName());
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[] { billingAddressType.FindPrimaryKey(), shippingAddressType.FindPrimaryKey(), orderPk, orderDetailsPk },
new[] { orderPk, billingAddressType.FindPrimaryKey(), shippingAddressType.FindPrimaryKey(), orderDetailsPk },
orderPkConstraint.MappedKeys);

var orderDetailsDate = orderDetailsType.FindProperty(nameof(OrderDetails.OrderDate));
Expand Down Expand Up @@ -710,15 +714,15 @@ public void Can_use_relational_model_with_SQL_queries()

Assert.True(orderMapping.IncludesDerivedTypes);
Assert.Equal(
new[] { nameof(Order.AlternateId), nameof(Order.CustomerId), nameof(Order.OrderDate), nameof(Order.OrderId) },
new[] { nameof(Order.AlternateId), nameof(Order.CustomerId), nameof(Order.Id), nameof(Order.OrderDate) },
orderMapping.ColumnMappings.Select(m => m.Property.Name));

var ordersQuery = orderMapping.SqlQuery;
Assert.Equal(
new[] { orderType },
ordersQuery.EntityTypeMappings.Select(m => m.EntityType));
Assert.Equal(
new[] { nameof(Order.CustomerId), nameof(Order.OrderDate), nameof(Order.OrderId), "SomeName" },
new[] { nameof(Order.CustomerId), nameof(Order.Id), nameof(Order.OrderDate), "SomeName" },
ordersQuery.Columns.Select(m => m.Name));
Assert.Equal("Microsoft.EntityFrameworkCore.Metadata.RelationalModelTest+Order.MappedSqlQuery", ordersQuery.Name);
Assert.Null(ordersQuery.Schema);
Expand Down Expand Up @@ -791,7 +795,7 @@ public void Can_use_relational_model_with_functions()

Assert.True(orderMapping.IncludesDerivedTypes);
Assert.Equal(
new[] { nameof(Order.AlternateId), nameof(Order.CustomerId), nameof(Order.OrderDate), nameof(Order.OrderId) },
new[] { nameof(Order.AlternateId), nameof(Order.CustomerId), nameof(Order.Id), nameof(Order.OrderDate) },
orderMapping.ColumnMappings.Select(m => m.Property.Name));

var ordersFunction = orderMapping.StoreFunction;
Expand All @@ -800,7 +804,7 @@ public void Can_use_relational_model_with_functions()
new[] { orderType },
ordersFunction.EntityTypeMappings.Select(m => m.EntityType));
Assert.Equal(
new[] { nameof(Order.CustomerId), nameof(Order.OrderDate), nameof(Order.OrderId), "SomeName" },
new[] { nameof(Order.CustomerId), nameof(Order.Id), nameof(Order.OrderDate), "SomeName" },
ordersFunction.Columns.Select(m => m.Name));
Assert.Equal("GetOrders", ordersFunction.Name);
Assert.Null(ordersFunction.Schema);
Expand Down Expand Up @@ -879,7 +883,7 @@ private class SpecialCustomer : Customer

private class Order
{
public int OrderId { get; set; }
public int Id { get; set; }
public Guid AlternateId { get; set; }

public DateTime OrderDate { get; set; }
Expand Down

0 comments on commit f7805c8

Please sign in to comment.