Skip to content

Commit

Permalink
Remove annotations key when overriden explicitly
Browse files Browse the repository at this point in the history
Don't try to scaffold alternate keys for owned types

Fixes #15698
Fixes #13628
Fixes #12414
  • Loading branch information
AndriySvyryd committed Aug 29, 2019
1 parent a6760b6 commit 2a5c747
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions EFCore.Runtime.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"src\\EFCore.Sqlite.NTS\\EFCore.Sqlite.NTS.csproj",
"src\\EFCore.Sqlite\\EFCore.Sqlite.csproj",
"src\\EFCore\\EFCore.csproj",
"src\\Microsoft.Data.Sqlite.Core\\Microsoft.Data.Sqlite.Core.csproj",
"test\\EFCore.Analyzers.Tests\\EFCore.Analyzers.Test.csproj",
"test\\EFCore.Cosmos.FunctionalTests\\EFCore.Cosmos.FunctionalTests.csproj",
"test\\EFCore.Cosmos.Tests\\EFCore.Cosmos.Tests.csproj",
Expand Down
13 changes: 8 additions & 5 deletions src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,15 @@ protected virtual void GenerateKeys(
GenerateKey(builderName, primaryKey, stringBuilder, primary: true);
}

foreach (var key in keys.Where(
key => key != primaryKey
&& (!key.GetReferencingForeignKeys().Any()
|| key.GetAnnotations().Any())))
if (primaryKey?.DeclaringEntityType.IsOwned() != true)
{
GenerateKey(builderName, key, stringBuilder);
foreach (var key in keys.Where(
key => key != primaryKey
&& (!key.GetReferencingForeignKeys().Any()
|| key.GetAnnotations().Any())))
{
GenerateKey(builderName, key, stringBuilder);
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public virtual InternalKeyBuilder PrimaryKey(

if (previousPrimaryKey?.Builder != null)
{
RemoveKeyIfUnused(previousPrimaryKey);
RemoveKeyIfUnused(previousPrimaryKey, configurationSource);
}
}
}
Expand Down Expand Up @@ -1530,13 +1530,13 @@ private IEnumerable<ForeignKey> FindConflictingRelationships(
detachedRelationships.AddRange(
key.GetReferencingForeignKeys().ToList()
.Select(DetachRelationship));
var removed = HasNoKey(key, configurationSource);
var removed = key.DeclaringEntityType.Builder.HasNoKey(key, configurationSource);
Debug.Assert(removed != null);
}

foreach (var index in property.GetContainingIndexes().ToList())
{
var removed = HasNoIndex(index, configurationSource);
var removed = index.DeclaringEntityType.Builder.HasNoIndex(index, configurationSource);
Debug.Assert(removed != null);
}

Expand Down Expand Up @@ -1706,7 +1706,7 @@ public static EntityType.Snapshot DetachAllMembers([NotNull] EntityType entityTy
return new EntityType.Snapshot(entityType, detachedProperties, detachedIndexes, detachedKeys, detachedRelationships);
}

private void RemoveKeyIfUnused(Key key)
private void RemoveKeyIfUnused(Key key, ConfigurationSource configurationSource = ConfigurationSource.Convention)
{
if (Metadata.FindPrimaryKey() == key)
{
Expand All @@ -1718,7 +1718,7 @@ private void RemoveKeyIfUnused(Key key)
return;
}

HasNoKey(key, ConfigurationSource.Convention);
HasNoKey(key, configurationSource);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -124,6 +125,7 @@ private class EntityWithOneProperty

private class EntityWithTwoProperties
{
[Key]
public int Id { get; set; }
public int AlternateId { get; set; }
public EntityWithOneProperty EntityWithOneProperty { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,12 @@ public void Does_not_match_PK_name_properties_if_subset_of_dependent_PK_and_cont
public void Matches_PK_name_properties_if_subset_of_dependent_PK()
{
var dependentTypeBuilder = DependentType.Builder;
dependentTypeBuilder.PrimaryKey(new[] { DependentEntity.PrincipalEntityPeEKaYProperty }, ConfigurationSource.Explicit);
var pkProperty = dependentTypeBuilder.Property(
DependentEntity.IDProperty, ConfigurationSource.Convention)
.IsRequired(true, ConfigurationSource.Convention)
.Metadata;
var fkProperty = dependentTypeBuilder.Property(
DependentEntity.PeEKaYProperty, ConfigurationSource.Convention)
DependentEntity.PrincipalEntityPeEKaYProperty, ConfigurationSource.Convention)
.Metadata;

dependentTypeBuilder.PrimaryKey(new[] { pkProperty, fkProperty }, ConfigurationSource.Explicit);
Expand Down

0 comments on commit 2a5c747

Please sign in to comment.