Skip to content

Commit

Permalink
Merge branch 'release/5.0-rc2' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers committed Sep 23, 2020
2 parents fbc1bcd + 7253dbc commit 095841e
Show file tree
Hide file tree
Showing 23 changed files with 113 additions and 83 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
34 changes: 29 additions & 5 deletions src/EFCore.Relational/Extensions/RelationalPropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,28 @@ 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>
[Obsolete("Use the overload that takes a StoreObjectIdentifier")]
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 +98,23 @@ 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>
[Obsolete("Use the overload that takes a StoreObjectIdentifier")]
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 +194,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
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
model =>
{
var entitType = model.FindEntityType("TestNamespace.Entity");
Assert.Equal("propertyA", entitType.GetProperty("A").GetColumnName());
Assert.Equal("propertyA", entitType.GetProperty("A").GetColumnBaseName());
Assert.Equal("nchar(10)", entitType.GetProperty("B").GetColumnType());
Assert.Equal("random", entitType.GetProperty("C").GetColumnName());
Assert.Equal("random", entitType.GetProperty("C").GetColumnBaseName());
Assert.Equal("varchar(200)", entitType.GetProperty("C").GetColumnType());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void Loads_column_types()
},
col1 =>
{
Assert.Equal("created", col1.GetColumnName());
Assert.Equal("created", col1.GetColumnBaseName());
Assert.Equal(ValueGenerated.OnAdd, col1.ValueGenerated);
},
col2 =>
Expand All @@ -221,12 +221,12 @@ public void Loads_column_types()
},
col3 =>
{
Assert.Equal("modified", col3.GetColumnName());
Assert.Equal("modified", col3.GetColumnBaseName());
Assert.Equal(ValueGenerated.OnAddOrUpdate, col3.ValueGenerated);
},
col4 =>
{
Assert.Equal("occupation", col4.GetColumnName());
Assert.Equal("occupation", col4.GetColumnBaseName());
Assert.Equal(typeof(string), col4.ClrType);
Assert.False(col4.IsColumnNullable());
Assert.Null(col4.GetMaxLength());
Expand Down Expand Up @@ -495,7 +495,7 @@ public void Primary_key(string[] keyProps, int length)
var model = (EntityType)_factory.Create(info, new ModelReverseEngineerOptions()).GetEntityTypes().Single();

Assert.Equal("MyPk", model.FindPrimaryKey().GetName());
Assert.Equal(keyProps, model.FindPrimaryKey().Properties.Select(p => p.GetColumnName()).ToArray());
Assert.Equal(keyProps, model.FindPrimaryKey().Properties.Select(p => p.GetColumnBaseName()).ToArray());
}

[ConditionalFact]
Expand Down Expand Up @@ -1272,12 +1272,12 @@ public void Unique_names()
s1 =>
{
Assert.Equal("SanItized", s1.Name);
Assert.Equal("San itized", s1.GetColumnName());
Assert.Equal("San itized", s1.GetColumnBaseName());
},
s2 =>
{
Assert.Equal("SanItized1", s2.Name);
Assert.Equal("San+itized", s2.GetColumnName());
Assert.Equal("San+itized", s2.GetColumnBaseName());
});
},
ef2 =>
Expand All @@ -1286,7 +1286,7 @@ public void Unique_names()
Assert.Equal("EF1", ef2.Name);
var id = Assert.Single(ef2.GetProperties());
Assert.Equal("Id", id.Name);
Assert.Equal("Id", id.GetColumnName());
Assert.Equal("Id", id.GetColumnBaseName());
});
}

Expand Down Expand Up @@ -1931,7 +1931,7 @@ public void UseDatabaseNames_and_NoPluralize_work_together(
new ModelReverseEngineerOptions { UseDatabaseNames = useDatabaseNames, NoPluralize = noPluralize });

var user = Assert.Single(model.GetEntityTypes().Where(e => e.GetTableName() == userTableName));
var id = Assert.Single(user.GetProperties().Where(p => p.GetColumnName() == "id"));
var id = Assert.Single(user.GetProperties().Where(p => p.GetColumnBaseName() == "id"));
var foreignKey = Assert.Single(user.GetReferencingForeignKeys());
if (useDatabaseNames && noPluralize)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public virtual void Missing_concurrency_token_property_is_created_on_the_base_ty
var concurrencyProperty = animal.FindProperty("_TableSharingConcurrencyTokenConvention_Version");
Assert.True(concurrencyProperty.IsConcurrencyToken);
Assert.True(concurrencyProperty.IsShadowProperty());
Assert.Equal("Version", concurrencyProperty.GetColumnName());
Assert.Equal("Version", concurrencyProperty.GetColumnBaseName());
Assert.Equal(ValueGenerated.OnAddOrUpdate, concurrencyProperty.ValueGenerated);
}

Expand Down Expand Up @@ -118,7 +118,7 @@ public virtual void Missing_concurrency_token_properties_are_created_on_the_base
var concurrencyProperty = animal.FindProperty("_TableSharingConcurrencyTokenConvention_Version");
Assert.True(concurrencyProperty.IsConcurrencyToken);
Assert.True(concurrencyProperty.IsShadowProperty());
Assert.Equal("Version", concurrencyProperty.GetColumnName());
Assert.Equal("Version", concurrencyProperty.GetColumnBaseName());
Assert.Equal(ValueGenerated.OnUpdate, concurrencyProperty.ValueGenerated);

var cat = model.FindEntityType(typeof(Cat));
Expand All @@ -128,7 +128,7 @@ public virtual void Missing_concurrency_token_properties_are_created_on_the_base
concurrencyProperty = animalHouse.FindProperty("_TableSharingConcurrencyTokenConvention_Version");
Assert.True(concurrencyProperty.IsConcurrencyToken);
Assert.True(concurrencyProperty.IsShadowProperty());
Assert.Equal("Version", concurrencyProperty.GetColumnName());
Assert.Equal("Version", concurrencyProperty.GetColumnBaseName());
Assert.Equal(ValueGenerated.OnUpdate, concurrencyProperty.ValueGenerated);

var theMovie = model.FindEntityType(typeof(TheMovie));
Expand All @@ -152,7 +152,7 @@ public virtual void Missing_concurrency_token_property_is_created_on_the_sharing
var concurrencyProperty = personEntityType.FindProperty("_TableSharingConcurrencyTokenConvention_Version");
Assert.True(concurrencyProperty.IsConcurrencyToken);
Assert.True(concurrencyProperty.IsShadowProperty());
Assert.Equal("Version", concurrencyProperty.GetColumnName());
Assert.Equal("Version", concurrencyProperty.GetColumnBaseName());
Assert.Equal(ValueGenerated.OnAddOrUpdate, concurrencyProperty.ValueGenerated);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void Can_set_column_name()
var property = modelBuilder.Model.FindEntityType(typeof(Customer)).FindProperty("Name");

Assert.Equal("Name", property.Name);
Assert.Equal("Eman", property.GetColumnName());
Assert.Equal("Eman", property.GetColumnBaseName());
}

[ConditionalFact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void Can_access_property()
Assert.NotNull(propertyBuilder.IsFixedLength(true));
Assert.True(propertyBuilder.Metadata.IsFixedLength());
Assert.NotNull(propertyBuilder.HasColumnName("Splew"));
Assert.Equal("Splew", propertyBuilder.Metadata.GetColumnName());
Assert.Equal("Splew", propertyBuilder.Metadata.GetColumnBaseName());
Assert.NotNull(propertyBuilder.HasColumnType("int"));
Assert.Equal("int", propertyBuilder.Metadata.GetColumnType());
Assert.NotNull(propertyBuilder.HasDefaultValue(1));
Expand All @@ -106,7 +106,7 @@ public void Can_access_property()
Assert.False(propertyBuilder.Metadata.IsFixedLength());
Assert.NotNull(propertyBuilder.HasColumnName("Splow", fromDataAnnotation: true));
Assert.Null(propertyBuilder.HasColumnName("Splod"));
Assert.Equal("Splow", propertyBuilder.Metadata.GetColumnName());
Assert.Equal("Splow", propertyBuilder.Metadata.GetColumnBaseName());
Assert.NotNull(propertyBuilder.HasColumnType("varchar", fromDataAnnotation: true));
Assert.Null(propertyBuilder.HasColumnType("int"));
Assert.Equal("varchar", propertyBuilder.Metadata.GetColumnType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ public void Can_get_and_set_column_name()
.Property(e => e.Name)
.Metadata;

Assert.Equal("Name", property.GetColumnName());
Assert.Equal("Name", property.GetColumnBaseName());

property.SetColumnName("Eman");

Assert.Equal("Name", property.Name);
Assert.Equal("Eman", property.GetColumnName());
Assert.Equal("Eman", property.GetColumnBaseName());

property.SetColumnName(null);

Assert.Equal("Name", property.GetColumnName());
Assert.Equal("Name", property.GetColumnBaseName());
}

[ConditionalFact]
Expand Down
Loading

0 comments on commit 095841e

Please sign in to comment.