Skip to content

Commit

Permalink
[5.0.3] Add column name null checks in default FK and PK name calcula…
Browse files Browse the repository at this point in the history
…tion (#23697)

* Add column name null checks in default FK and PK name calculation

Fixes #23672

* Add quirk mode for #23672
  • Loading branch information
AndriySvyryd authored Jan 13, 2021
1 parent d382ab9 commit 90d3f8e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/EFCore.Relational/Extensions/RelationalIndexExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ 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;
}

var useOldBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23672", out var enabled) && enabled;
var rootIndex = index;

// Limit traversal to avoid getting stuck in a cycle (validation will throw for these later)
Expand All @@ -101,7 +102,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 || useOldBehavior)
&& otherColumnNames.SequenceEqual(columnNames))
{
linkedIndex = otherIndex;
break;
Expand All @@ -125,7 +128,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
12 changes: 8 additions & 4 deletions src/EFCore.Relational/Extensions/RelationalKeyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -97,12 +98,13 @@ 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;
}

var useOldBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23672", out var enabled) && enabled;
var rootKey = key;

// Limit traversal to avoid getting stuck in a cycle (validation will throw for these later)
Expand All @@ -114,7 +116,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 || useOldBehavior)
&& otherColumnNames.SequenceEqual(columnNames))
{
linkedKey = otherKey;
break;
Expand All @@ -138,7 +142,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 90d3f8e

Please sign in to comment.