-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Problem seeding data that contains owned type #12004
Comments
@thiagomajesk Owned types must be seeded with a HasData call after the OwnsOne call. Also, since owned types by convention have a primary key generated in shadow state, and since seed data requires keys to be defined, then this requires use of an anonymous type and setting the key. For example: public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Street { get; set; }
public string Zip { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>(b =>
{
b.HasData(new
{
Id = 1,
Name = "Spin"
});
b.OwnsOne(e => e.Address).HasData(new
{
BlogId = 1,
Street = "11 Meadow Drive",
Zip = "DN37 7RU"
});
});
} Note that this would become easier if navigations were supported for seeding, which is tracked by #10000. |
@ajcvickers Thanks for the explanation, that's in line with the docs regarding how owned types work using shadow properties. However, I think we could benefit from a little bit of clarity describing this scenario on the seeding section. What do you think? |
@thiagomajesk Agreed. Filed dotnet/EntityFramework.Docs#710 |
@ajcvickers Is there a way to seed without explicitly providing |
Hello...
If seed the database with the sample data bellow using
HasData
I'll get the following error:Sample data:
It seems that EF is not recognizing my
position
property as an owned type even though I have configured it so:modelBuilder.Entity<Location>().OwnsOne(l => l.Position);
.The text was updated successfully, but these errors were encountered: