Skip to content

Commit

Permalink
Don't throw when SqlServer ValueGenerationStrategy is configured on a…
Browse files Browse the repository at this point in the history
…n incompatible property by a convention

Fixes #7208
  • Loading branch information
AndriySvyryd committed Dec 22, 2016
1 parent 2c3d59a commit 748bcf6
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public RelationalPropertyBuilderAnnotations(
/// </summary>
protected override bool ShouldThrowOnConflict => false;

/// <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>
protected override bool ShouldThrowOnInvalidConfiguration => Annotations.ConfigurationSource == ConfigurationSource.Explicit;

/// <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 @@ -13,8 +13,6 @@ namespace Microsoft.EntityFrameworkCore.Metadata
{
public class RelationalPropertyAnnotations : IRelationalPropertyAnnotations
{
protected readonly RelationalFullAnnotationNames ProviderFullAnnotationNames;

public RelationalPropertyAnnotations([NotNull] IProperty property,
[CanBeNull] RelationalFullAnnotationNames providerFullAnnotationNames)
: this(new RelationalAnnotations(property), providerFullAnnotationNames)
Expand All @@ -28,9 +26,12 @@ protected RelationalPropertyAnnotations([NotNull] RelationalAnnotations annotati
ProviderFullAnnotationNames = providerFullAnnotationNames;
}

public virtual RelationalFullAnnotationNames ProviderFullAnnotationNames { get; }

protected virtual RelationalAnnotations Annotations { get; }
protected virtual IProperty Property => (IProperty)Annotations.Metadata;
protected virtual bool ShouldThrowOnConflict => true;
protected virtual bool ShouldThrowOnInvalidConfiguration => true;

public virtual string ColumnName
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,24 @@ public SqlServerPropertyBuilderAnnotations(

private InternalPropertyBuilder PropertyBuilder => ((Property)Property).Builder;

/// <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>
protected new virtual RelationalAnnotationsBuilder Annotations => (RelationalAnnotationsBuilder)base.Annotations;

/// <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>
protected override bool ShouldThrowOnConflict => false;

/// <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>
protected override bool ShouldThrowOnInvalidConfiguration => Annotations.ConfigurationSource == ConfigurationSource.Explicit;

#pragma warning disable 109
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
Expand Down Expand Up @@ -91,27 +103,29 @@ public SqlServerPropertyBuilderAnnotations(
/// </summary>
public new virtual bool ValueGenerationStrategy(SqlServerValueGenerationStrategy? value)
{
if (!SetValueGenerationStrategy(value))
{
return false;
}

if (value == null)
{
PropertyBuilder.ValueGenerated(ValueGenerated.Never, ConfigurationSource.Convention);
PropertyBuilder.RequiresValueGenerator(false, ConfigurationSource.Convention);
HiLoSequenceName(null);
HiLoSequenceSchema(null);
}
else if (value.Value == SqlServerValueGenerationStrategy.IdentityColumn)
{
PropertyBuilder.ValueGenerated(ValueGenerated.OnAdd, ConfigurationSource.Convention);
PropertyBuilder.RequiresValueGenerator(true, ConfigurationSource.Convention);
HiLoSequenceName(null);
HiLoSequenceSchema(null);
}
else if (value.Value == SqlServerValueGenerationStrategy.SequenceHiLo)
{
PropertyBuilder.ValueGenerated(ValueGenerated.OnAdd, ConfigurationSource.Convention);
PropertyBuilder.RequiresValueGenerator(true, ConfigurationSource.Convention);
}

return SetValueGenerationStrategy(value);
return true;
}
#pragma warning restore 109
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public virtual SqlServerValueGenerationStrategy? ValueGenerationStrategy
[param: CanBeNull] set { SetValueGenerationStrategy(value); }
}

private SqlServerValueGenerationStrategy? GetSqlServerValueGenerationStrategy(bool fallbackToModel)
public virtual SqlServerValueGenerationStrategy? GetSqlServerValueGenerationStrategy(bool fallbackToModel)
{
if (GetDefaultValue(false) != null
|| GetDefaultValueSql(false) != null
Expand Down Expand Up @@ -124,15 +124,25 @@ protected virtual bool SetValueGenerationStrategy(SqlServerValueGenerationStrate
if (value == SqlServerValueGenerationStrategy.IdentityColumn
&& !IsCompatibleIdentityColumn(propertyType))
{
throw new ArgumentException(SqlServerStrings.IdentityBadType(
Property.Name, Property.DeclaringEntityType.DisplayName(), propertyType.ShortDisplayName()));
if (ShouldThrowOnInvalidConfiguration)
{
throw new ArgumentException(SqlServerStrings.IdentityBadType(
Property.Name, Property.DeclaringEntityType.DisplayName(), propertyType.ShortDisplayName()));
}

return false;
}

if (value == SqlServerValueGenerationStrategy.SequenceHiLo
&& !IsCompatibleSequenceHiLo(propertyType))
{
throw new ArgumentException(SqlServerStrings.SequenceBadType(
Property.Name, Property.DeclaringEntityType.DisplayName(), propertyType.ShortDisplayName()));
if (ShouldThrowOnInvalidConfiguration)
{
throw new ArgumentException(SqlServerStrings.SequenceBadType(
Property.Name, Property.DeclaringEntityType.DisplayName(), propertyType.ShortDisplayName()));
}

return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

using System;
using System.Linq;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Xunit;

Expand Down Expand Up @@ -70,6 +72,38 @@ public void Can_access_property()
a => a.Name.StartsWith(SqlServerAnnotationNames.Prefix, StringComparison.Ordinal)));
}

[Fact]
public void Throws_setting_sequence_generation_for_invalid_type_only_with_explicit()
{
var propertyBuilder = CreateBuilder()
.Entity(typeof(Splot), ConfigurationSource.Convention)
.Property("Name", typeof(string), ConfigurationSource.Convention);

Assert.False(propertyBuilder.SqlServer(ConfigurationSource.Convention)
.ValueGenerationStrategy(SqlServerValueGenerationStrategy.SequenceHiLo));

Assert.Equal(
SqlServerStrings.SequenceBadType("Name", nameof(Splot), "string"),
Assert.Throws<ArgumentException>(
() => propertyBuilder.SqlServer(ConfigurationSource.Explicit).ValueGenerationStrategy(SqlServerValueGenerationStrategy.SequenceHiLo)).Message);
}

[Fact]
public void Throws_setting_identity_generation_for_invalid_type_only_with_explicit()
{
var propertyBuilder = CreateBuilder()
.Entity(typeof(Splot), ConfigurationSource.Convention)
.Property("Name", typeof(string), ConfigurationSource.Convention);

Assert.False(propertyBuilder.SqlServer(ConfigurationSource.Convention)
.ValueGenerationStrategy(SqlServerValueGenerationStrategy.IdentityColumn));

Assert.Equal(
SqlServerStrings.IdentityBadType("Name", nameof(Splot), "string"),
Assert.Throws<ArgumentException>(
() => propertyBuilder.SqlServer(ConfigurationSource.Explicit).ValueGenerationStrategy(SqlServerValueGenerationStrategy.IdentityColumn)).Message);
}

[Fact]
public void Can_access_key()
{
Expand Down

0 comments on commit 748bcf6

Please sign in to comment.