Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove previous primary key when overriden #17481

Merged
merged 1 commit into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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