Skip to content

Commit

Permalink
Metadata: Don't override KeylessAttribute with KeyAttribute
Browse files Browse the repository at this point in the history
Part of #20014
  • Loading branch information
smitpatel committed Feb 24, 2020
1 parent aa57b4e commit 5305551
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/EFCore/Metadata/Conventions/KeyAttributeConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ protected override void ProcessPropertyAdded(
return;
}

if (entityType.IsKeyless
&& entityType.GetIsKeylessConfigurationSource().Overrides(ConfigurationSource.DataAnnotation))
{
// TODO: Log a warning that KeyAttribute is being ignored. See issue#20014
// This code path will also be hit when entity is marked as Keyless explicitly
return;
}

var entityTypeBuilder = entityType.Builder;
var currentKey = entityTypeBuilder.Metadata.FindPrimaryKey();
var properties = new List<string> { propertyBuilder.Metadata.Name };
Expand Down
6 changes: 6 additions & 0 deletions src/EFCore/Metadata/IConventionEntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public interface IConventionEntityType : IEntityType, IConventionTypeBase
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
void HasNoKey(bool? keyless, bool fromDataAnnotation = false);

/// <summary>
/// Returns the configuration source for the IsKeyless property.
/// </summary>
/// <returns> The configuration source for the IsKeyless property. </returns>
ConfigurationSource? GetIsKeylessConfigurationSource();

/// <summary>
/// Sets the primary key for this entity type.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public void KeyAttribute_overrides_keyless_attribute()

var entityBuilder = modelBuilder.Entity<KeyClash>();

Assert.False(entityBuilder.Metadata.IsKeyless);
Assert.NotNull(entityBuilder.Metadata.FindPrimaryKey());
Assert.True(entityBuilder.Metadata.IsKeyless);
Assert.Null(entityBuilder.Metadata.FindPrimaryKey());
}

#endregion
Expand Down

0 comments on commit 5305551

Please sign in to comment.