Skip to content
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
27 changes: 27 additions & 0 deletions src/EFCore.Design/Migrations/Internal/SnapshotModelProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public SnapshotModelProcessor(
ProcessElement(element.DependentToPrincipal, version);
ProcessElement(element.PrincipalToDependent, version);
}

ProcessComplexProperties(entityType, version);
}
}

Expand Down Expand Up @@ -110,6 +112,31 @@ private void ProcessElement(IReadOnlyEntityType entityType, string version)
}
}

private void ProcessComplexProperties(IReadOnlyTypeBase typeBase, string version)
{
foreach (var complexProperty in typeBase.GetComplexProperties())
{
ProcessElement(complexProperty, version);

if (complexProperty is IMutableComplexProperty mutableComplexProperty)
{
UpdateComplexPropertyNullability(mutableComplexProperty, version);
}

ProcessComplexProperties(complexProperty.ComplexType, version);
}
}

private static void UpdateComplexPropertyNullability(IMutableComplexProperty complexProperty, string version)
{
if ((version.StartsWith("8.", StringComparison.Ordinal)
|| version.StartsWith("9.", StringComparison.Ordinal))
&& !complexProperty.ClrType.IsNullableType())
{
complexProperty.IsNullable = false;
}
}

private void ProcessElement(IReadOnlyAnnotatable? metadata, string version)
{
if (version.StartsWith("1.", StringComparison.Ordinal)
Expand Down
5 changes: 4 additions & 1 deletion src/ef/Commands/DbContextOptimizeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ protected override int Execute(string[] args)
_precompileQueries!.HasValue(),
_nativeAot!.HasValue());

ReportResults(result);
if (result != null)
{
ReportResults(result);
}

return base.Execute(args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,98 @@ public void Can_diff_against_older_sequence_model(Type snapshotType)
AssertSameSnapshot(snapshotType, context);
}

[ConditionalFact]
public void Updates_complex_property_nullability_for_pre_10_snapshots()
{
var builder = new ModelBuilder();
var model = builder.Model;
((Model)model).SetProductVersion("9.0.0");

var entityType = builder.Entity<EntityWithComplexProperty>();
entityType.ComplexProperty(e => e.StructComplexProperty, b =>
{
b.Property(c => c.Value);
});

var complexProperty = entityType.Metadata.GetComplexProperties().Single();
Assert.Equal(typeof(StructComplexType), complexProperty.ClrType);

var complexPropertyInternal = (ComplexProperty)complexProperty;
Assert.Null(complexPropertyInternal.GetIsNullableConfigurationSource());
Assert.False(complexProperty.IsNullable);

var reporter = new TestOperationReporter();
var processor = new SnapshotModelProcessor(reporter, DummyModelRuntimeInitializer.Instance);
processor.Process(model);

Assert.NotNull(complexPropertyInternal.GetIsNullableConfigurationSource());
Assert.False(complexProperty.IsNullable);
Assert.Empty(reporter.Messages);
}

[ConditionalFact]
public void Does_not_update_complex_property_nullability_for_10_or_later_snapshots()
{
var builder = new ModelBuilder();
var model = builder.Model;
((Model)model).SetProductVersion("10.0.0");

var entityType = builder.Entity<EntityWithComplexProperty>();
entityType.ComplexProperty(e => e.StructComplexProperty, b =>
{
b.Property(c => c.Value);
});

var complexProperty = entityType.Metadata.GetComplexProperties().Single();
var complexPropertyInternal = (ComplexProperty)complexProperty;

Assert.Null(complexPropertyInternal.GetIsNullableConfigurationSource());

var reporter = new TestOperationReporter();
var processor = new SnapshotModelProcessor(reporter, DummyModelRuntimeInitializer.Instance);
processor.Process(model);

Assert.Null(complexPropertyInternal.GetIsNullableConfigurationSource());
Assert.Empty(reporter.Messages);
}

[ConditionalFact]
public void Updates_nested_complex_property_nullability_for_pre_10_snapshots()
{
var builder = new ModelBuilder();
var model = builder.Model;
((Model)model).SetProductVersion("9.0.0");

var entityType = builder.Entity<EntityWithNestedComplexProperty>();
entityType.ComplexProperty(e => e.OuterComplexProperty, b =>
{
b.Property(c => c.Value);
b.ComplexProperty(c => c.InnerComplexProperty, b2 =>
{
b2.Property(c2 => c2.Value);
});
});

var outerComplexProperty = entityType.Metadata.GetComplexProperties().Single();
var innerComplexProperty = outerComplexProperty.ComplexType.GetComplexProperties().Single();

var outerComplexPropertyInternal = (ComplexProperty)outerComplexProperty;
var innerComplexPropertyInternal = (ComplexProperty)innerComplexProperty;

Assert.Null(outerComplexPropertyInternal.GetIsNullableConfigurationSource());
Assert.Null(innerComplexPropertyInternal.GetIsNullableConfigurationSource());

var reporter = new TestOperationReporter();
var processor = new SnapshotModelProcessor(reporter, DummyModelRuntimeInitializer.Instance);
processor.Process(model);

Assert.NotNull(outerComplexPropertyInternal.GetIsNullableConfigurationSource());
Assert.False(outerComplexProperty.IsNullable);
Assert.NotNull(innerComplexPropertyInternal.GetIsNullableConfigurationSource());
Assert.False(innerComplexProperty.IsNullable);
Assert.Empty(reporter.Messages);
}

private static void AssertSameSnapshot(Type snapshotType, DbContext context)
{
var differ = context.GetService<IMigrationsModelDiffer>();
Expand Down Expand Up @@ -363,6 +455,34 @@ private class BlogDetails
public ICollection<Post> Posts { get; set; }
}

private class EntityWithComplexProperty
{
public int Id { get; set; }
public StructComplexType StructComplexProperty { get; set; }
}

private struct StructComplexType
{
public int Value { get; set; }
}

private class EntityWithNestedComplexProperty
{
public int Id { get; set; }
public OuterStructComplexType OuterComplexProperty { get; set; }
}

private struct OuterStructComplexType
{
public int Value { get; set; }
public InnerStructComplexType InnerComplexProperty { get; set; }
}

private struct InnerStructComplexType
{
public int Value { get; set; }
}

private class OwnershipModelSnapshot2_0 : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
Expand Down
Loading