Closed vyruz1986 closed 9 months ago
@vyruz1986 This requires using an anonymous type, since the Product type can't be created publicly without setting the navigation property. Something like this:
modelBuilder.Entity<Product>(builder =>
{
builder.OwnsOne(p => p.Price, price =>
{
price.OwnsOne(p => p.Amount, priceAmount =>
{
priceAmount.Property(p => p.Value)
.HasPrecision(2, 20)
.IsRequired();
priceAmount.Property(p => p.Currency)
.HasDefaultValue(Currency.EUR)
.IsUnicode(false)
.HasMaxLength(3)
.IsRequired();
priceAmount.HasData(new { PriceProductId = 1, Value = 1m, Currency = Currency.EUR });
});
price.HasData(new { ProductId = 1 });
});
builder.HasData(new { Id = 1 });
});
Ah I see, I was still trying to seed the Product
data with the actual type, instead of also using an anonymous there as well.
This fixed it for me, thanks!
I'm failing to figure out how to configure data seeding on nested owned entities. I've read through #31373 and #14359 and am aware of the current issues about EFCore not being able to seed data on the top-level entity (tracked in #10000). What those issues have not yet discussed, is seeding data across nested owned types. I have made a minimal example (also attached below) which configures a
Product
>Price
>MonetaryAmount
relation.Creating a migration without the data seeding works, and the generated SQL looks good in that it wants to create a single table with a column for each of the properties of the owned types, but when I add the
HasData()
statements and try to create a migration, I'm given the following error:Include your code
Full example available here: EFCoreNestedOwnedTypesDataSeeding.zip
Stack traces
Verbose output
Include provider and version information
EF Core version: Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 8.0 Operating system: Windows 11 23H2 build 22631.2861 IDE: VSCode 1.85.1