Skip to content

Commit

Permalink
Add column name null checks in default FK and PK name calculation
Browse files Browse the repository at this point in the history
Fixes #23672
  • Loading branch information
AndriySvyryd committed Dec 16, 2020
1 parent 0e91fa1 commit 75bdf3d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/EFCore.Relational/Extensions/RelationalIndexExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public static string GetDefaultName([NotNull] this IIndex index)
/// <returns> The default name that would be used for this index. </returns>
public static string GetDefaultDatabaseName([NotNull] this IIndex index, in StoreObjectIdentifier storeObject)
{
var propertyNames = index.Properties.GetColumnNames(storeObject);
if (propertyNames == null)
var columnNames = index.Properties.GetColumnNames(storeObject);
if (columnNames == null)
{
return null;
}
Expand All @@ -101,7 +101,9 @@ public static string GetDefaultDatabaseName([NotNull] this IIndex index, in Stor
.FindRowInternalForeignKeys(storeObject)
.SelectMany(fk => fk.PrincipalEntityType.GetIndexes()))
{
if (otherIndex.Properties.GetColumnNames(storeObject).SequenceEqual(propertyNames))
var otherColumnNames = otherIndex.Properties.GetColumnNames(storeObject);
if (otherColumnNames != null
&& otherColumnNames.SequenceEqual(columnNames))
{
linkedIndex = otherIndex;
break;
Expand All @@ -125,7 +127,7 @@ public static string GetDefaultDatabaseName([NotNull] this IIndex index, in Stor
.Append("IX_")
.Append(storeObject.Name)
.Append("_")
.AppendJoin(propertyNames, "_")
.AppendJoin(columnNames, "_")
.ToString();

return Uniquifier.Truncate(baseName, index.DeclaringEntityType.Model.GetMaxIdentifierLength());
Expand Down
10 changes: 6 additions & 4 deletions src/EFCore.Relational/Extensions/RelationalKeyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public static string GetDefaultName([NotNull] this IKey key, in StoreObjectIdent
}
else
{
var propertyNames = key.Properties.GetColumnNames(storeObject);
if (propertyNames == null)
var columnNames = key.Properties.GetColumnNames(storeObject);
if (columnNames == null)
{
return null;
}
Expand All @@ -114,7 +114,9 @@ public static string GetDefaultName([NotNull] this IKey key, in StoreObjectIdent
.FindRowInternalForeignKeys(storeObject)
.SelectMany(fk => fk.PrincipalEntityType.GetKeys()))
{
if (otherKey.Properties.GetColumnNames(storeObject).SequenceEqual(propertyNames))
var otherColumnNames = otherKey.Properties.GetColumnNames(storeObject);
if (otherColumnNames != null
&& otherColumnNames.SequenceEqual(columnNames))
{
linkedKey = otherKey;
break;
Expand All @@ -138,7 +140,7 @@ public static string GetDefaultName([NotNull] this IKey key, in StoreObjectIdent
.Append("AK_")
.Append(storeObject.Name)
.Append("_")
.AppendJoin(propertyNames, "_")
.AppendJoin(columnNames, "_")
.ToString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,46 @@ public virtual void Passes_for_compatible_duplicate_index_names_within_hierarchy
Assert.Equal(index1.GetDatabaseName(), index2.GetDatabaseName());
}

[ConditionalFact]
public virtual void Passes_for_indexes_on_related_types_mapped_to_different_tables()
{
var modelBuilder = CreateConventionalModelBuilder();
modelBuilder.Entity<PropertyBase>();
modelBuilder.Entity<Property>();

Validate(modelBuilder.Model);
}

[Table("Objects")]
private abstract class PropertyBase
{
public int Id { get; set; }

public Organization Organization { get; set; }
}

private class Organization
{
public int Id { get; set; }
}

[Table("Properties")]
private class Property : PropertyBase
{
public PropertyDetails Details { get; set; } = null!;
}

[Owned]
private class PropertyDetails
{
public Address Address { get; set; }
}

private class Address
{
public int Id { get; set; }
}

[ConditionalFact]
public virtual void Detects_missing_concurrency_token_on_the_base_type_without_convention()
{
Expand Down

0 comments on commit 75bdf3d

Please sign in to comment.