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

Problem seeding data that contains owned type #12004

Closed
thiagomajesk opened this issue May 14, 2018 · 5 comments
Closed

Problem seeding data that contains owned type #12004

thiagomajesk opened this issue May 14, 2018 · 5 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@thiagomajesk
Copy link

thiagomajesk commented May 14, 2018

Hello...
If seed the database with the sample data bellow using HasData I'll get the following error:

The seed entity for entity type 'Location' with the key value 'Id:1' cannot be added because it has the navigation 'Position' set. To seed relationships you need to add the related entity seed to 'Position' and specify the foreign key values {'LocationId'}.

Sample data:

{
    "id": 1,
    "name": "Second Avenue",
    "position": {
      "x": 0,
      "y": 0,
      "z": 0
    }
  }

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);.

@ajcvickers
Copy link
Member

@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 ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label May 16, 2018
@thiagomajesk
Copy link
Author

@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?

@ajcvickers
Copy link
Member

@thiagomajesk Agreed. Filed dotnet/EntityFramework.Docs#710

@Deilan
Copy link
Contributor

Deilan commented Jul 26, 2018

@ajcvickers Is there a way to seed without explicitly providing Id?

@ajcvickers
Copy link
Member

@Deilan Not currently. In this case, #10000 would allow that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants