Skip to content

Commit

Permalink
Don't compare seed values for store generated properties.
Browse files Browse the repository at this point in the history
Use the default value for non-nullable properties.

Fixes #18592
  • Loading branch information
AndriySvyryd committed Oct 25, 2019
1 parent 6148e6b commit 735e577
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,8 @@ protected virtual void DiffData(
{
var sourceProperty = diffContext.FindSource(targetProperty);
if (sourceProperty == null
|| !sourceEntityType.GetProperties().Contains(sourceProperty))
|| !sourceEntityType.GetProperties().Contains(sourceProperty)
|| targetProperty.ValueGenerated != ValueGenerated.Never)
{
continue;
}
Expand Down Expand Up @@ -1830,7 +1831,16 @@ var modelValuesChanged
var convertedType = sourceConverter?.ProviderClrType
?? targetConverter?.ProviderClrType;

var storeValuesChanged = convertedSourceValue?.GetType().UnwrapNullableType() != convertedTargetValue?.GetType().UnwrapNullableType();
if (convertedType != null
&& !convertedType.IsNullableType())
{
var defaultValue = convertedType.GetDefaultValue();
convertedSourceValue ??= defaultValue;
convertedTargetValue ??= defaultValue;
}

var storeValuesChanged = convertedSourceValue?.GetType().UnwrapNullableType()
!= convertedTargetValue?.GetType().UnwrapNullableType();

if (!storeValuesChanged
&& convertedType != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6719,6 +6719,68 @@ public void SeedData_nonkey_refactoring_value_conversion()
Assert.Empty);
}

[ConditionalFact]
public void SeedData_nonkey_refactoring_value_conversion_to_value_type()
{
Execute(
common => common.Entity(
"EntityWithOneProperty",
x => x.Property<int>("Id")),
source => source.Entity(
"EntityWithOneProperty",
x =>
{
x.Property<DateTime>("Value1");
x.HasData(
new { Id = 42 });
}),
target => target.Entity(
"EntityWithOneProperty",
x =>
{
x.Property<byte[]>("Value1")
.IsRequired()
.HasConversion(e => new DateTime(), e => new byte[0]);
x.HasData(
new { Id = 42 });
}),
Assert.Empty,
Assert.Empty);
}

[ConditionalFact]
public void SeedData_nonkey_refactoring_value_conversion_to_value_type_store_generated()
{
Execute(
common => common.Entity(
"EntityWithOneProperty",
x => x.Property<int>("Id")),
source => source.Entity(
"EntityWithOneProperty",
x =>
{
x.Property<DateTime>("Value1")
.ValueGeneratedOnAddOrUpdate()
.IsConcurrencyToken();
x.HasData(
new { Id = 42, Value1 = DateTime.Now });
}),
target => target.Entity(
"EntityWithOneProperty",
x =>
{
x.Property<byte[]>("Value1")
.IsRequired()
.ValueGeneratedOnAddOrUpdate()
.IsConcurrencyToken()
.HasConversion(e => new DateTime(), e => new byte[0]);
x.HasData(
new { Id = 42, Value1 = new byte[0] });
}),
Assert.Empty,
Assert.Empty);
}

[ConditionalFact]
public void SeedData_nonkey_refactoring_value_conversion_with_structural_provider_type()
{
Expand Down

0 comments on commit 735e577

Please sign in to comment.