Skip to content

Commit

Permalink
Propagate identity seed and increment to sharing columns
Browse files Browse the repository at this point in the history
Fixes #22358
  • Loading branch information
AndriySvyryd committed Sep 3, 2020
1 parent 3002db3 commit a6f0c52
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
48 changes: 42 additions & 6 deletions src/EFCore.SqlServer/Extensions/SqlServerPropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,25 @@ public static class SqlServerPropertyExtensions
/// <param name="property"> The property. </param>
/// <returns> The name to use for the hi-lo sequence. </returns>
public static string GetHiLoSequenceName([NotNull] this IProperty property)
=> (string)property[SqlServerAnnotationNames.HiLoSequenceName];

/// <summary>
/// Returns the name to use for the hi-lo sequence.
/// </summary>
/// <param name="property"> The property. </param>
/// <param name="storeObject"> The identifier of the store object. </param>
/// <returns> The name to use for the hi-lo sequence. </returns>
public static string GetHiLoSequenceName([NotNull] this IProperty property, in StoreObjectIdentifier storeObject)
{
var annotation = property.FindAnnotation(SqlServerAnnotationNames.HiLoSequenceName);
if (annotation != null)
{
return (string)annotation.Value;
}

var sharedTableRootProperty = property.FindSharedStoreObjectRootProperty(
StoreObjectIdentifier.Table(property.DeclaringEntityType.GetTableName(), property.DeclaringEntityType.GetSchema()));
var sharedTableRootProperty = property.FindSharedStoreObjectRootProperty(storeObject);
return sharedTableRootProperty != null
? sharedTableRootProperty.GetHiLoSequenceSchema()
? sharedTableRootProperty.GetHiLoSequenceName(storeObject)
: null;
}

Expand Down Expand Up @@ -82,17 +90,25 @@ public static string SetHiLoSequenceName(
/// <param name="property"> The property. </param>
/// <returns> The schema to use for the hi-lo sequence. </returns>
public static string GetHiLoSequenceSchema([NotNull] this IProperty property)
=> (string)property[SqlServerAnnotationNames.HiLoSequenceSchema];

/// <summary>
/// Returns the schema to use for the hi-lo sequence.
/// </summary>
/// <param name="property"> The property. </param>
/// <param name="storeObject"> The identifier of the store object. </param>
/// <returns> The schema to use for the hi-lo sequence. </returns>
public static string GetHiLoSequenceSchema([NotNull] this IProperty property, in StoreObjectIdentifier storeObject)
{
var annotation = property.FindAnnotation(SqlServerAnnotationNames.HiLoSequenceSchema);
if (annotation != null)
{
return (string)annotation.Value;
}

var sharedTableRootProperty = property.FindSharedStoreObjectRootProperty(
StoreObjectIdentifier.Table(property.DeclaringEntityType.GetTableName(), property.DeclaringEntityType.GetSchema()));
var sharedTableRootProperty = property.FindSharedStoreObjectRootProperty(storeObject);
return sharedTableRootProperty != null
? sharedTableRootProperty.GetHiLoSequenceSchema()
? sharedTableRootProperty.GetHiLoSequenceSchema(storeObject)
: null;
}

Expand Down Expand Up @@ -137,6 +153,7 @@ public static string SetHiLoSequenceSchema(
/// <summary>
/// Finds the <see cref="ISequence" /> in the model to use for the hi-lo pattern.
/// </summary>
/// <param name="property"> The property. </param>
/// <returns> The sequence to use, or <see langword="null" /> if no sequence exists in the model. </returns>
public static ISequence FindHiLoSequence([NotNull] this IProperty property)
{
Expand All @@ -151,6 +168,25 @@ public static ISequence FindHiLoSequence([NotNull] this IProperty property)
return model.FindSequence(sequenceName, sequenceSchema);
}

/// <summary>
/// Finds the <see cref="ISequence" /> in the model to use for the hi-lo pattern.
/// </summary>
/// <param name="property"> The property. </param>
/// <param name="storeObject"> The identifier of the store object. </param>
/// <returns> The sequence to use, or <see langword="null" /> if no sequence exists in the model. </returns>
public static ISequence FindHiLoSequence([NotNull] this IProperty property, in StoreObjectIdentifier storeObject)
{
var model = property.DeclaringEntityType.Model;

var sequenceName = property.GetHiLoSequenceName(storeObject)
?? model.GetHiLoSequenceName();

var sequenceSchema = property.GetHiLoSequenceSchema(storeObject)
?? model.GetHiLoSequenceSchema();

return model.FindSequence(sequenceName, sequenceSchema);
}

/// <summary>
/// Returns the identity seed.
/// </summary>
Expand Down
12 changes: 6 additions & 6 deletions src/EFCore.SqlServer/Internal/SqlServerModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ protected override void ValidateCompatible(
switch (propertyStrategy)
{
case SqlServerValueGenerationStrategy.IdentityColumn:
var increment = property.GetIdentityIncrement();
var duplicateIncrement = duplicateProperty.GetIdentityIncrement();
var increment = property.GetIdentityIncrement(storeObject);
var duplicateIncrement = duplicateProperty.GetIdentityIncrement(storeObject);
if (increment != duplicateIncrement)
{
throw new InvalidOperationException(
Expand All @@ -307,8 +307,8 @@ protected override void ValidateCompatible(
storeObject.DisplayName()));
}

var seed = property.GetIdentitySeed();
var duplicateSeed = duplicateProperty.GetIdentitySeed();
var seed = property.GetIdentitySeed(storeObject);
var duplicateSeed = duplicateProperty.GetIdentitySeed(storeObject);
if (seed != duplicateSeed)
{
throw new InvalidOperationException(
Expand All @@ -323,8 +323,8 @@ protected override void ValidateCompatible(

break;
case SqlServerValueGenerationStrategy.SequenceHiLo:
if (property.GetHiLoSequenceName() != duplicateProperty.GetHiLoSequenceName()
|| property.GetHiLoSequenceSchema() != duplicateProperty.GetHiLoSequenceSchema())
if (property.GetHiLoSequenceName(storeObject) != duplicateProperty.GetHiLoSequenceName(storeObject)
|| property.GetHiLoSequenceSchema(storeObject) != duplicateProperty.GetHiLoSequenceSchema(storeObject))
{
throw new InvalidOperationException(
SqlServerStrings.DuplicateColumnSequenceMismatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public virtual SqlServerSequenceValueGeneratorState GetOrAddSequenceState(
IProperty property,
IRelationalConnection connection)
{
var sequence = property.FindHiLoSequence();
var sequence = property.FindHiLoSequence(
StoreObjectIdentifier.Create(property.DeclaringEntityType, StoreObjectType.Table).Value);

Check.DebugAssert(sequence != null, "sequence is null");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.Logging;
using Xunit;

// ReSharper disable InconsistentNaming
Expand Down Expand Up @@ -111,6 +110,17 @@ public virtual void Detects_duplicate_column_names_within_hierarchy_with_differe
modelBuilder.Model);
}

[ConditionalFact]
public virtual void Passes_for_identity_seed_and_increment_on_owner()
{
var modelBuilder = CreateConventionalModelBuilder();
modelBuilder.Entity<Animal>().Property(a => a.Id).UseIdentityColumn(2, 3);
modelBuilder.Entity<Cat>().OwnsOne(a => a.FavoritePerson);
modelBuilder.Entity<Dog>();

Validate(modelBuilder.Model);
}

[ConditionalFact]
public virtual void Passes_for_duplicate_column_names_with_HiLoSequence()
{
Expand Down

0 comments on commit a6f0c52

Please sign in to comment.