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

Don't mark any composite key property as value generated on non-owned types. #17116

Merged
merged 1 commit into from
Aug 13, 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
10 changes: 9 additions & 1 deletion src/EFCore/Metadata/Conventions/ValueGenerationConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,19 @@ public virtual void ProcessEntityTypeBaseTypeChanged(
/// <returns> The store value generation strategy to set for the given property. </returns>
public static ValueGenerated? GetValueGenerated([NotNull] IProperty property)
=> !property.IsForeignKey()
&& property.FindContainingPrimaryKey()?.Properties.Count(p => !p.IsForeignKey()) == 1
&& ShouldHaveGeneratedProperty(property.FindContainingPrimaryKey())
&& CanBeGenerated(property)
? ValueGenerated.OnAdd
: (ValueGenerated?)null;

private static bool ShouldHaveGeneratedProperty(IKey key)
{
var onOwnedType = key?.DeclaringEntityType.IsOwned();
return key != null
&& (onOwnedType.Value && key.Properties.Count(p => !p.IsForeignKey()) == 1
|| !onOwnedType.Value && key.Properties.Count == 1);
}

/// <summary>
/// Indicates whether the specified property can have the value generated by the store or by a non-temporary value generator
/// when not set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.TestModels.Northwind;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;
Expand Down
14 changes: 13 additions & 1 deletion test/EFCore.Tests/ModelBuilding/OneToManyTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,7 @@ public virtual void Creates_overlapping_foreign_keys_with_different_nullability(
{
var modelBuilder = CreateModelBuilder();
var model = modelBuilder.Model;
modelBuilder.Ignore<OrderDetails>();
modelBuilder.Entity<Order>(
eb =>
{
Expand All @@ -2078,8 +2079,12 @@ public virtual void Creates_overlapping_foreign_keys_with_different_nullability(
{
eb.HasOne(p => p.Order).WithMany(o => o.Products).HasForeignKey("CommonId", "OrderId");
eb.HasOne<ProductCategory>().WithMany(c => c.Products).HasForeignKey("CommonId", "Category").IsRequired();

eb.HasKey("Id", "CommonId");
});

modelBuilder.FinalizeModel();

var dependentType = model.FindEntityType(typeof(Product));

var optionalFk = dependentType.GetNavigations().Single().ForeignKey;
Expand All @@ -2089,6 +2094,9 @@ public virtual void Creates_overlapping_foreign_keys_with_different_nullability(
var requiredFk = dependentType.GetForeignKeys().Single(foreignKey => foreignKey != optionalFk);
Assert.True(requiredFk.IsRequired);
Assert.False(requiredFk.Properties.Last().IsNullable);

var dependentKey = dependentType.FindPrimaryKey();
Assert.True(dependentKey.Properties.All(p => p.ValueGenerated == ValueGenerated.Never));
}

[ConditionalFact]
Expand Down Expand Up @@ -2434,9 +2442,11 @@ public virtual void Handles_identity_correctly_while_removing_navigation()
modelBuilder.Ignore<Delta>();
modelBuilder.Entity<Epsilon>().HasOne<Alpha>().WithMany(b => b.Epsilons);

var property = modelBuilder.Model.FindEntityType(typeof(Epsilon)).FindProperty("Id");
Assert.Equal(ValueGenerated.Never, property.ValueGenerated);

modelBuilder.FinalizeModel();

var property = modelBuilder.Model.FindEntityType(typeof(Epsilon)).FindProperty("Id");
Assert.Equal(ValueGenerated.Never, property.ValueGenerated);
}

Expand Down Expand Up @@ -2801,6 +2811,8 @@ public virtual void Do_not_match_non_unique_FK_when_overlap_with_PK()

var fk = modelBuilder.Model.FindEntityType(typeof(CompositeChild)).GetForeignKeys().Single();
Assert.Equal("ParentId", fk.Properties[0].Name);

modelBuilder.FinalizeModel();
}

private static void AssertGraph(
Expand Down