Skip to content

Commit

Permalink
Add IEnumerable overloads of HasData to OwnedNavigationBuilders
Browse files Browse the repository at this point in the history
Fixes #19735
  • Loading branch information
ajcvickers committed Feb 2, 2020
1 parent a152828 commit 0151831
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/EFCore/Metadata/Builders/EntityTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ public virtual DataBuilder HasData([NotNull] params object[] data)
/// Configures this entity to have seed data. It is used to generate data motion migrations.
/// </summary>
/// <param name="data">
/// An array of seed data represented by anonymous types.
/// A collection of seed data represented by anonymous types.
/// </param>
/// <returns> An object that can be used to configure the model data. </returns>
public virtual DataBuilder HasData([NotNull] IEnumerable<object> data)
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore/Metadata/Builders/EntityTypeBuilder`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ public virtual DataBuilder<TEntity> HasData([NotNull] params TEntity[] data)
/// Configures this entity to have seed data. It is used to generate data motion migrations.
/// </summary>
/// <param name="data">
/// An array of seed data of the same type as the entity.
/// A collection of seed data of the same type as the entity.
/// </param>
/// <returns> An object that can be used to configure the model data. </returns>
public virtual DataBuilder<TEntity> HasData([NotNull] IEnumerable<TEntity> data)
Expand All @@ -738,7 +738,7 @@ public virtual DataBuilder<TEntity> HasData([NotNull] IEnumerable<TEntity> data)
/// Configures this entity to have seed data. It is used to generate data motion migrations.
/// </summary>
/// <param name="data">
/// An array of seed data represented by anonymous types.
/// A colection of seed data represented by anonymous types.
/// </param>
/// <returns> An object that can be used to configure the model data. </returns>
public new virtual DataBuilder<TEntity> HasData([NotNull] IEnumerable<object> data)
Expand Down
17 changes: 17 additions & 0 deletions src/EFCore/Metadata/Builders/OwnedNavigationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.ChangeTracking;
Expand Down Expand Up @@ -784,5 +785,21 @@ public virtual DataBuilder HasData([NotNull] params object[] data)

return new DataBuilder();
}

/// <summary>
/// Configures this entity to have seed data. It is used to generate data motion migrations.
/// </summary>
/// <param name="data">
/// A collection of seed data represented by anonymous types.
/// </param>
/// <returns> An object that can be used to configure the model data. </returns>
public virtual DataBuilder HasData([NotNull] IEnumerable<object> data)
{
Check.NotNull(data, nameof(data));

OwnedEntityType.AddData(data);

return new DataBuilder();
}
}
}
30 changes: 30 additions & 0 deletions src/EFCore/Metadata/Builders/OwnedNavigationBuilder`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,22 @@ public virtual DataBuilder<TDependentEntity> HasData([NotNull] params TDependent
return new DataBuilder<TDependentEntity>();
}

/// <summary>
/// Configures this entity to have seed data. It is used to generate data motion migrations.
/// </summary>
/// <param name="data">
/// A collection of seed data.
/// </param>
/// <returns> An object that can be used to configure the model data. </returns>
public virtual DataBuilder<TDependentEntity> HasData([NotNull] IEnumerable<TDependentEntity> data)
{
Check.NotNull(data, nameof(data));

OwnedEntityType.AddData(data);

return new DataBuilder<TDependentEntity>();
}

/// <summary>
/// Configures this entity to have seed data. It is used to generate data motion migrations.
/// </summary>
Expand All @@ -640,5 +656,19 @@ public virtual DataBuilder<TDependentEntity> HasData([NotNull] params TDependent

return new DataBuilder<TDependentEntity>();
}

/// <summary>
/// Configures this entity to have seed data. It is used to generate data motion migrations.
/// </summary>
/// <param name="data">
/// A collection of seed data represented by anonymous types.
/// </param>
/// <returns> An object that can be used to configure the model data. </returns>
public new virtual DataBuilder<TDependentEntity> HasData([NotNull] IEnumerable<object> data)
{
base.HasData(data);

return new DataBuilder<TDependentEntity>();
}
}
}
6 changes: 6 additions & 0 deletions test/EFCore.Tests/ModelBuilding/ModelBuilderGenericTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,12 @@ public override DataBuilder<TDependentEntity> HasData(params TDependentEntity[]
public override DataBuilder<TDependentEntity> HasData(params object[] data)
=> OwnedNavigationBuilder.HasData(data);

public override DataBuilder<TDependentEntity> HasData(IEnumerable<TDependentEntity> data)
=> OwnedNavigationBuilder.HasData(data);

public override DataBuilder<TDependentEntity> HasData(IEnumerable<object> data)
=> OwnedNavigationBuilder.HasData(data);

OwnedNavigationBuilder<TEntity, TDependentEntity> IInfrastructure<OwnedNavigationBuilder<TEntity, TDependentEntity>>.
Instance
=> OwnedNavigationBuilder;
Expand Down
12 changes: 12 additions & 0 deletions test/EFCore.Tests/ModelBuilding/ModelBuilderNonGenericTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,18 @@ public override DataBuilder<TDependentEntity> HasData(params object[] data)
return new DataBuilder<TDependentEntity>();
}

public override DataBuilder<TDependentEntity> HasData(IEnumerable<TDependentEntity> data)
{
OwnedNavigationBuilder.HasData(data);
return new DataBuilder<TDependentEntity>();
}

public override DataBuilder<TDependentEntity> HasData(IEnumerable<object> data)
{
OwnedNavigationBuilder.HasData(data);
return new DataBuilder<TDependentEntity>();
}

OwnedNavigationBuilder IInfrastructure<OwnedNavigationBuilder>.Instance => OwnedNavigationBuilder;
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/EFCore.Tests/ModelBuilding/ModelBuilderTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,10 @@ public abstract TestOwnedNavigationBuilder<TEntity, TDependentEntity> UsePropert
public abstract DataBuilder<TDependentEntity> HasData(params TDependentEntity[] data);

public abstract DataBuilder<TDependentEntity> HasData(params object[] data);

public abstract DataBuilder<TDependentEntity> HasData(IEnumerable<TDependentEntity> data);

public abstract DataBuilder<TDependentEntity> HasData(IEnumerable<object> data);
}
}
}
49 changes: 44 additions & 5 deletions test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,23 @@ public virtual void Can_configure_one_to_one_relationship_from_an_owned_type_col
Assert.Null(model.FindEntityType(typeof(SpecialOrder)));
}

[ConditionalFact]
public virtual void Can_configure_owned_type_from_an_owned_type_collection()
[Flags]
public enum HasDataOverload
{
Array = 0,
Enumerable = 1,
Generic = 2,
Params = 4
}

[ConditionalTheory]
[InlineData(HasDataOverload.Array)]
[InlineData(HasDataOverload.Array | HasDataOverload.Params)]
[InlineData(HasDataOverload.Array | HasDataOverload.Generic)]
[InlineData(HasDataOverload.Array | HasDataOverload.Params | HasDataOverload.Generic)]
[InlineData(HasDataOverload.Enumerable)]
[InlineData(HasDataOverload.Enumerable | HasDataOverload.Generic)]
public virtual void Can_configure_owned_type_from_an_owned_type_collection(HasDataOverload hasDataOverload)
{
var modelBuilder = CreateModelBuilder();

Expand All @@ -435,9 +450,33 @@ public virtual void Can_configure_owned_type_from_an_owned_type_collection()
c => c.Orders, ob =>
{
ob.HasKey(o => o.OrderId);
ob.OwnsOne(o => o.Details)
.HasData(
new OrderDetails { OrderId = -1 });
var ownedNavigationBuilder = ob.OwnsOne(o => o.Details);

switch (hasDataOverload)
{
case HasDataOverload.Array:
ownedNavigationBuilder.HasData(new object[] { new OrderDetails { OrderId = -1 } });
break;
case HasDataOverload.Array | HasDataOverload.Params:
ownedNavigationBuilder.HasData((object)new OrderDetails { OrderId = -1 });
break;
case HasDataOverload.Array | HasDataOverload.Generic:
// ReSharper disable once RedundantExplicitParamsArrayCreation
ownedNavigationBuilder.HasData(new[] { new OrderDetails { OrderId = -1 } });
break;
case HasDataOverload.Array | HasDataOverload.Params | HasDataOverload.Generic:
ownedNavigationBuilder.HasData(new OrderDetails { OrderId = -1 });
break;
case HasDataOverload.Enumerable:
ownedNavigationBuilder.HasData(new List<object> { new OrderDetails { OrderId = -1 } });
break;
case HasDataOverload.Enumerable | HasDataOverload.Generic:
ownedNavigationBuilder.HasData(new List<OrderDetails>() { new OrderDetails { OrderId = -1 } });
break;
default:
Assert.True(false, $"Unexpected HasData overload specification {hasDataOverload}");
break;
}
});

var model = modelBuilder.FinalizeModel();
Expand Down

0 comments on commit 0151831

Please sign in to comment.