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

SqlServer Migrations: Persist ValueGenerationStrategy.None in snapshot #22226

Merged
1 commit merged into from
Aug 27, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,17 @@ private IReadOnlyList<MethodCallCodeFragment> GenerateValueGenerationStrategy(
IDictionary<string, IAnnotation> annotations,
bool onModel)
{
var strategy = GetAndRemove<SqlServerValueGenerationStrategy>(SqlServerAnnotationNames.ValueGenerationStrategy);
SqlServerValueGenerationStrategy strategy;
if (annotations.TryGetValue(SqlServerAnnotationNames.ValueGenerationStrategy, out var strategyAnnotation)
&& strategyAnnotation.Value != null)
{
annotations.Remove(SqlServerAnnotationNames.ValueGenerationStrategy);
strategy = (SqlServerValueGenerationStrategy)strategyAnnotation.Value;
}
else
{
return Array.Empty<MethodCallCodeFragment>();
}

switch (strategy)
{
Expand Down Expand Up @@ -154,7 +164,13 @@ private IReadOnlyList<MethodCallCodeFragment> GenerateValueGenerationStrategy(
};

case SqlServerValueGenerationStrategy.None:
return Array.Empty<MethodCallCodeFragment>();
return new List<MethodCallCodeFragment>
{
new MethodCallCodeFragment(
nameof(ModelBuilder.HasAnnotation),
SqlServerAnnotationNames.ValueGenerationStrategy,
SqlServerValueGenerationStrategy.None)
};

default:
throw new ArgumentOutOfRangeException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
Expand Down Expand Up @@ -46,28 +47,58 @@ public virtual void ProcessModelFinalizing(
{
foreach (var property in entityType.GetDeclaredProperties())
{
var strategy = SqlServerValueGenerationStrategy.None;
// Needed for the annotation to show up in the model snapshot
SqlServerValueGenerationStrategy? strategy = null;
var table = entityType.GetTableName();
if (table != null)
{
strategy = property.GetValueGenerationStrategy(StoreObjectIdentifier.Table(table, entityType.GetSchema()));
var storeObject = StoreObjectIdentifier.Table(table, entityType.GetSchema());
strategy = property.GetValueGenerationStrategy(storeObject);
if (strategy == SqlServerValueGenerationStrategy.None
&& !IsStrategyNoneNeeded(property, storeObject))
{
strategy = null;
}
}
else
{
var view = entityType.GetViewName();
if (view != null)
{
strategy = property.GetValueGenerationStrategy(StoreObjectIdentifier.View(view, entityType.GetViewSchema()));
var storeObject = StoreObjectIdentifier.View(view, entityType.GetViewSchema());
strategy = property.GetValueGenerationStrategy(storeObject);
if (strategy == SqlServerValueGenerationStrategy.None
&& !IsStrategyNoneNeeded(property, storeObject))
{
strategy = null;
}
}
}

if (strategy != SqlServerValueGenerationStrategy.None)
// Needed for the annotation to show up in the model snapshot
if (strategy != null)
{
property.Builder.HasValueGenerationStrategy(strategy);
}
}
}

static bool IsStrategyNoneNeeded(IProperty property, StoreObjectIdentifier storeObject)
{
if (property.ValueGenerated == ValueGenerated.OnAdd
&& property.GetDefaultValue(storeObject) == null
&& property.GetDefaultValueSql(storeObject) == null
&& property.GetComputedColumnSql(storeObject) == null
&& property.DeclaringEntityType.Model.GetValueGenerationStrategy() == SqlServerValueGenerationStrategy.IdentityColumn)
{
var providerClrType = (property.GetValueConverter() ?? property.FindRelationalTypeMapping(storeObject)?.Converter)
?.ProviderClrType.UnwrapNullableType();

return providerClrType != null
&& (providerClrType.IsInteger() || providerClrType == typeof(decimal));
}

return false;
}
}
}
}
40 changes: 40 additions & 0 deletions test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,46 @@ public virtual void Property_ValueGenerated_value_is_stored_in_snapshot()
o => Assert.Equal(ValueGenerated.OnAdd, o.GetEntityTypes().First().FindProperty("AlternateId").ValueGenerated));
}

[ConditionalFact]
public virtual void Property_ValueGenerated_non_identity()
{
Test(
modelBuilder => modelBuilder.Entity<EntityWithEnumType>(
x =>
{
x.Property(e => e.Id).Metadata.SetValueGenerationStrategy(SqlServerValueGenerationStrategy.None);
x.Property(e => e.Day).ValueGeneratedOnAdd();
}),
AddBoilerPlate(
GetHeading()
+ @"
modelBuilder.Entity(""Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithEnumType"", b =>
{
b.Property<int>(""Id"")
.ValueGeneratedOnAdd()
.HasColumnType(""int"")
.HasAnnotation(""SqlServer:ValueGenerationStrategy"", SqlServerValueGenerationStrategy.None);

b.Property<long>(""Day"")
.ValueGeneratedOnAdd()
.HasColumnType(""bigint"")
.HasAnnotation(""SqlServer:ValueGenerationStrategy"", SqlServerValueGenerationStrategy.None);

b.HasKey(""Id"");

b.ToTable(""EntityWithEnumType"");
});"),
model =>
{
var id = model.GetEntityTypes().Single().GetProperty(nameof(EntityWithEnumType.Id));
Assert.Equal(ValueGenerated.OnAdd, id.ValueGenerated);
Assert.Equal(SqlServerValueGenerationStrategy.None, id.GetValueGenerationStrategy());
var day = model.GetEntityTypes().Single().GetProperty(nameof(EntityWithEnumType.Day));
Assert.Equal(ValueGenerated.OnAdd, day.ValueGenerated);
Assert.Equal(SqlServerValueGenerationStrategy.None, day.GetValueGenerationStrategy());
});
}

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