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

Override existing explicitly configured values when moving annotations on making owned type shared #29114

Merged
merged 1 commit into from
Sep 15, 2022
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
2 changes: 1 addition & 1 deletion src/EFCore/Infrastructure/AnnotatableBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public virtual AnnotatableBuilder<TMetadata, TModelBuilder> MergeAnnotationsFrom
annotation.Name,
annotation.Value,
configurationSource,
canOverrideSameSource: false)
canOverrideSameSource: true)
?? builder;
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/EFCore.Tests/ModelBuilding/ModelBuilderGenericTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ public override TestPropertyBuilder<TProperty> IsRequired(bool isRequired = true
public override TestPropertyBuilder<TProperty> HasMaxLength(int maxLength)
=> Wrap(PropertyBuilder.HasMaxLength(maxLength));

public override TestPropertyBuilder<TProperty> HasPrecision(int precision)
=> Wrap(PropertyBuilder.HasPrecision(precision));

public override TestPropertyBuilder<TProperty> HasPrecision(int precision, int scale)
=> Wrap(PropertyBuilder.HasPrecision(precision, scale));

Expand Down
3 changes: 3 additions & 0 deletions test/EFCore.Tests/ModelBuilding/ModelBuilderNonGenericTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,9 @@ public override TestPropertyBuilder<TProperty> IsRequired(bool isRequired = true
public override TestPropertyBuilder<TProperty> HasMaxLength(int maxLength)
=> Wrap(PropertyBuilder.HasMaxLength(maxLength));

public override TestPropertyBuilder<TProperty> HasPrecision(int precision)
=> Wrap(PropertyBuilder.HasPrecision(precision));

public override TestPropertyBuilder<TProperty> HasPrecision(int precision, int scale)
=> Wrap(PropertyBuilder.HasPrecision(precision, scale));

Expand Down
1 change: 1 addition & 0 deletions test/EFCore.Tests/ModelBuilding/ModelBuilderTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ public abstract class TestPropertyBuilder<TProperty>
public abstract TestPropertyBuilder<TProperty> HasAnnotation(string annotation, object? value);
public abstract TestPropertyBuilder<TProperty> IsRequired(bool isRequired = true);
public abstract TestPropertyBuilder<TProperty> HasMaxLength(int maxLength);
public abstract TestPropertyBuilder<TProperty> HasPrecision(int precision);
public abstract TestPropertyBuilder<TProperty> HasPrecision(int precision, int scale);
public abstract TestPropertyBuilder<TProperty> IsUnicode(bool unicode = true);
public abstract TestPropertyBuilder<TProperty> IsRowVersion();
Expand Down
94 changes: 94 additions & 0 deletions test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,100 @@ public abstract partial class ModelBuilderTest
{
public abstract class OwnedTypesTestBase : ModelBuilderTestBase
{
[ConditionalTheory] // Issue #28091
[InlineData(16, 2, 16, 4, 16, 4, 16, 4, 16, 4)]
[InlineData(16, 2, 17, 4, 17, 4, 17, 4, 17, 4)]
[InlineData(null, null, 16, 4, 16, 4, 16, 4, 16, 4)]
[InlineData(null, null, 16, 4, 15, 3, 14, 2, 13, 1)]
[InlineData(null, null, 16, null, 15, null, 14, null, 13, null)]
[InlineData(17, null, 16, null, 15, null, 14, null, 13, null)]
[InlineData(17, 5, 16, 4, 15, 3, 14, 2, 13, 1)]
[InlineData(17, 5, null, null, null, null, null, null, null, null)]
public virtual void Precision_and_scale_for_property_type_used_in_owned_types_can_be_overwritten(
int? defaultPrecision,
int? defaultScale,
int? mainPrecision,
int? mainScale,
int? otherPrecision,
int? otherScale,
int? onePrecision,
int? oneScale,
int? manyPrecision,
int? manyScale)
{
var modelBuilder = CreateModelBuilder(
c =>
{
if (defaultPrecision.HasValue)
{
if (defaultScale.HasValue)
{
c.Properties<decimal>().HavePrecision(defaultPrecision.Value, defaultScale.Value);
}
else
{
c.Properties<decimal>().HavePrecision(defaultPrecision.Value);
}
}
});

modelBuilder.Entity<MainOtter>(
b =>
{
HasPrecision(b.Property(x => x.Number), mainPrecision, mainScale);
b.OwnsOne(
b => b.OwnedEntity, b =>
{
HasPrecision(b.Property(x => x.Number), onePrecision, oneScale);
});
});

modelBuilder.Entity<OtherOtter>(
b =>
{
HasPrecision(b.Property(x => x.Number), otherPrecision, otherScale);
b.OwnsMany(
b => b.OwnedEntities, b =>
{
HasPrecision(b.Property(x => x.Number), manyPrecision, manyScale);
});
});

var model = modelBuilder.FinalizeModel();

var mainType = model.FindEntityType(typeof(MainOtter))!;
var otherType = model.FindEntityType(typeof(OtherOtter))!;
var oneType = model.FindEntityType(typeof(OwnedOtter), nameof(MainOtter.OwnedEntity), mainType)!;
var manyType = model.FindEntityType(typeof(OwnedOtter), nameof(OtherOtter.OwnedEntities), otherType)!;

Assert.Equal(mainPrecision ?? defaultPrecision, mainType.FindProperty(nameof(MainOtter.Number))!.GetPrecision());
Assert.Equal(mainScale ?? defaultScale, mainType.FindProperty(nameof(MainOtter.Number))!.GetScale());

Assert.Equal(otherPrecision ?? defaultPrecision, otherType.FindProperty(nameof(OtherOtter.Number))!.GetPrecision());
Assert.Equal(otherScale ?? defaultScale, otherType.FindProperty(nameof(OtherOtter.Number))!.GetScale());

Assert.Equal(onePrecision ?? defaultPrecision, oneType.FindProperty(nameof(OwnedOtter.Number))!.GetPrecision());
Assert.Equal(oneScale ?? defaultScale, oneType.FindProperty(nameof(OwnedOtter.Number))!.GetScale());

Assert.Equal(manyPrecision ?? defaultPrecision, manyType.FindProperty(nameof(OwnedOtter.Number))!.GetPrecision());
Assert.Equal(manyScale ?? defaultScale, manyType.FindProperty(nameof(OwnedOtter.Number))!.GetScale());

void HasPrecision(TestPropertyBuilder<decimal> testPropertyBuilder, int? precision, int? scale)
{
if (precision.HasValue)
{
if (scale.HasValue)
{
testPropertyBuilder.HasPrecision(precision.Value, scale.Value);
}
else
{
testPropertyBuilder.HasPrecision(precision.Value);
}
}
}
}

[ConditionalFact]
public virtual void Can_configure_owned_type()
{
Expand Down
19 changes: 19 additions & 0 deletions test/EFCore.Tests/ModelBuilding/TestModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,4 +1232,23 @@ protected class Discount
public int? StoreId { get; set; }
public Store? Store { get; set; }
}

protected class MainOtter
{
public Guid Id { get; set; }
public decimal Number { get; set; }
public OwnedOtter OwnedEntity { get; set; } = null!;
}

protected class OtherOtter
{
public Guid Id { get; set; }
public decimal Number { get; set; }
public List<OwnedOtter> OwnedEntities { get; } = new();
}

protected class OwnedOtter
{
public decimal Number { get; set; }
}
}