Skip to content

Commit

Permalink
Merge branch 'rel/1.1.1' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd committed Dec 16, 2016
2 parents 36597ec + cb79bb8 commit fa79083
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Xunit;

Expand Down Expand Up @@ -754,6 +755,29 @@ public class Toy
public string Name { get; set; }
}

[Fact]
public virtual ModelBuilder Key_specified_on_multiple_properties_can_be_overriden()
{
var modelBuilder = CreateModelBuilder();

modelBuilder.Entity<CompositeKeyAttribute>().HasKey(c => new {c.IdRow, c.Name});

Validate(modelBuilder);

Assert.Equal(2, modelBuilder.Model.FindEntityType(typeof(CompositeKeyAttribute)).GetKeys().Single().Properties.Count);

return modelBuilder;
}

private class CompositeKeyAttribute
{
[Key]
public int IdRow { get; set; }

[Key]
public string Name { get; set; }
}

[Fact]
public virtual ModelBuilder DatabaseGeneratedOption_configures_the_property_correctly()
{
Expand All @@ -769,6 +793,7 @@ public virtual ModelBuilder DatabaseGeneratedOption_configures_the_property_corr

var identity = entity.FindProperty(nameof(GeneratedEntity.Identity));
Assert.Equal(ValueGenerated.OnAdd, identity.ValueGenerated);
Assert.False(identity.RequiresValueGenerator);

var version = entity.FindProperty(nameof(GeneratedEntity.Version));
Assert.Equal(ValueGenerated.OnAddOrUpdate, version.ValueGenerated);
Expand Down Expand Up @@ -1418,9 +1443,11 @@ public virtual void InversePropertyAttribute_removes_ambiguity_with_base_type()

modelBuilder.Entity<SpecialBookLabel>().HasBaseType((Type)null);

Assert.Null(model.FindEntityType(typeof(SpecialBookLabel)).FindNavigation(nameof(SpecialBookLabel.Book))?.FindInverse());
Assert.Null(model.FindEntityType(typeof(BookLabel)).FindNavigation(nameof(SpecialBookLabel.Book)));
Assert.Null(model.FindEntityType(typeof(Book)).FindNavigation(nameof(Book.Label)));
Assert.Null(model.FindEntityType(typeof(Book)).FindNavigation(nameof(Book.AlternateLabel)));
Assert.Null(model.FindEntityType(typeof(BookLabel)).FindNavigation(nameof(SpecialBookLabel.Book)));
Assert.Null(model.FindEntityType(typeof(SpecialBookLabel)).FindNavigation(nameof(SpecialBookLabel.Book)));
Assert.Null(model.FindEntityType(typeof(AnotherBookLabel)).FindNavigation(nameof(SpecialBookLabel.Book)));
}

[Fact]
Expand All @@ -1447,9 +1474,8 @@ public virtual void InversePropertyAttribute_from_ignored_base_causes_ambiguity(
modelBuilder.Entity<SpecialBookLabel>();
modelBuilder.Ignore<BookLabel>();

Assert.Null(model.FindEntityType(typeof(BookLabel)));
Assert.Null(model.FindEntityType(typeof(AnotherBookLabel)).FindNavigation(nameof(AnotherBookLabel.Book)).FindInverse());
Assert.Null(model.FindEntityType(typeof(SpecialBookLabel)).FindNavigation(nameof(SpecialBookLabel.Book)).FindInverse());
Assert.Null(model.FindEntityType(typeof(AnotherBookLabel)).FindNavigation(nameof(AnotherBookLabel.Book)));
Assert.Null(model.FindEntityType(typeof(SpecialBookLabel)).FindNavigation(nameof(SpecialBookLabel.Book)));
Assert.Equal(0, model.FindEntityType(typeof(Book)).GetNavigations().Count());
}

Expand Down Expand Up @@ -1523,6 +1549,48 @@ private class AnotherBookLabel : BookLabel
{
}

[Fact]
public virtual void InversePropertyAttribute_removes_ambiguity_with_base_type_bidirectional()
{
var modelBuilder = CreateModelBuilder();
var qEntity = modelBuilder.Entity<Q>().Metadata;

Assert.Equal(nameof(P.QRef), qEntity.FindNavigation(nameof(Q.PRef)).FindInverse().Name);
Assert.Equal(nameof(E.QRefDerived), qEntity.FindNavigation(nameof(Q.ERef)).FindInverse().Name);
Assert.Equal(qEntity.GetDeclaredForeignKeys().Count(), qEntity.GetDeclaredIndexes().Count());

var pEntity = modelBuilder.Model.FindEntityType(typeof(P));
Assert.Equal(pEntity.GetDeclaredForeignKeys().Count(), pEntity.GetDeclaredIndexes().Count());

var eEntity = modelBuilder.Model.FindEntityType(typeof(E));
Assert.Equal(eEntity.GetDeclaredForeignKeys().Count(), eEntity.GetDeclaredIndexes().Count());
}

public class Q
{
public int Id { get; set; }

[InverseProperty(nameof(E.QRefDerived))]
public virtual E ERef { get; set; }

[InverseProperty(nameof(P.QRef))]
public virtual P PRef { get; set; }
}

public class P
{
public int Id { get; set; }

[InverseProperty(nameof(Q.PRef))]
public virtual Q QRef { get; set; }
}

public class E : P
{
[InverseProperty(nameof(Q.ERef))]
public virtual Q QRefDerived { get; set; }
}

[Fact]
public virtual void ForeignKeyAttribute_creates_two_relationships_if_applied_on_property_on_both_side()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
// 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.Reflection;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.ComponentModel.DataAnnotations.Schema;

namespace Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal
{
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public class SqlServerValueGenerationStrategyConvention : DatabaseGeneratedAttributeConvention, IModelConvention
public class SqlServerValueGenerationStrategyConvention : IModelConvention
{
public override InternalPropertyBuilder Apply(
InternalPropertyBuilder propertyBuilder, DatabaseGeneratedAttribute attribute, MemberInfo clrMember)
{
propertyBuilder.SqlServer(ConfigurationSource.DataAnnotation).ValueGenerationStrategy(
attribute.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity
? SqlServerValueGenerationStrategy.IdentityColumn
: (SqlServerValueGenerationStrategy?)null);

return base.Apply(propertyBuilder, attribute, clrMember);
}

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public override ConventionSet AddConventions(ConventionSet conventionSet)
var valueGenerationStrategyConvention = new SqlServerValueGenerationStrategyConvention();
conventionSet.ModelInitializedConventions.Add(valueGenerationStrategyConvention);

ReplaceConvention(conventionSet.PropertyAddedConventions, (DatabaseGeneratedAttributeConvention)valueGenerationStrategyConvention);

var sqlServerInMemoryTablesConvention = new SqlServerMemoryOptimizedTablesConvention();
conventionSet.EntityTypeAnnotationSetConventions.Add(sqlServerInMemoryTablesConvention);

Expand All @@ -48,8 +46,6 @@ public override ConventionSet AddConventions(ConventionSet conventionSet)

conventionSet.IndexAnnotationSetConventions.Add(sqlServerIndexConvention);

ReplaceConvention(conventionSet.PropertyFieldChangedConventions, (DatabaseGeneratedAttributeConvention)valueGenerationStrategyConvention);

conventionSet.PropertyNullableChangedConventions.Add(sqlServerIndexConvention);

conventionSet.PropertyAnnotationSetConventions.Add(sqlServerIndexConvention);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,19 @@ public virtual InternalRelationshipBuilder Apply(InternalRelationshipBuilder rel
// Try to use PK properties if principal end is not ambiguous
if (foreignKey.IsUnique
&& !foreignKey.IsSelfReferencing()
&& !ConfigurationSource.Convention.Overrides(foreignKey.GetPrincipalEndConfigurationSource())
&& foreignKey.DeclaringEntityType.BaseType == null)
{
foreignKeyProperties = GetCompatiblePrimaryKeyProperties(
foreignKey.DeclaringEntityType,
foreignKey.PrincipalEntityType,
foreignKey.PrincipalKey.Properties);
if (!ConfigurationSource.Convention.Overrides(foreignKey.GetPrincipalEndConfigurationSource()))
{
foreignKeyProperties = GetCompatiblePrimaryKeyProperties(
foreignKey.DeclaringEntityType,
foreignKey.PrincipalEntityType,
foreignKey.PrincipalKey.Properties);
}
else
{
foreignKeyProperties = FindCandidateForeignKeyProperties(foreignKey, onDependent: true, matchPK: true);
}
}
}

Expand Down Expand Up @@ -128,7 +134,7 @@ public virtual InternalRelationshipBuilder Apply(InternalRelationshipBuilder rel
return relationshipBuilder;
}

private IReadOnlyList<Property> FindCandidateForeignKeyProperties(ForeignKey foreignKey, bool onDependent)
private IReadOnlyList<Property> FindCandidateForeignKeyProperties(ForeignKey foreignKey, bool onDependent, bool matchPK = false)
{
var baseNames = new List<string>();
var navigation = onDependent
Expand All @@ -148,7 +154,7 @@ private IReadOnlyList<Property> FindCandidateForeignKeyProperties(ForeignKey for

foreach (var baseName in baseNames)
{
var match = FindMatchingProperties(foreignKey, baseName, onDependent);
var match = FindMatchingProperties(foreignKey, baseName, onDependent, matchPK);
if (match != null)
{
return match;
Expand Down Expand Up @@ -176,7 +182,7 @@ private static IReadOnlyList<Property> GetCompatiblePrimaryKeyProperties(EntityT
}

private IReadOnlyList<Property> FindMatchingProperties(
ForeignKey foreignKey, string baseName, bool onDependent)
ForeignKey foreignKey, string baseName, bool onDependent, bool matchPK = false)
{
var dependentEntityType = onDependent
? foreignKey.DeclaringEntityType
Expand Down Expand Up @@ -243,7 +249,8 @@ private IReadOnlyList<Property> FindMatchingProperties(
foreach (var key in dependentEntityType.GetKeys())
{
if (key.Properties.All(property => foreignKeyProperties.Contains(property))
&& (!foreignKey.IsUnique || key.IsPrimaryKey()))
&& (!foreignKey.IsUnique
|| (key.IsPrimaryKey() && !matchPK)))
{
return null;
}
Expand Down
Loading

0 comments on commit fa79083

Please sign in to comment.