Efcore: Data Seeding: Add support for navigations

Created on 6 Oct 2017  路  18Comments  路  Source: dotnet/efcore

This depends on #9914

area-migrations-seeding type-enhancement

Most helpful comment

It's in the backlog, so it probably will be done after 2019. Remember to vote :+1: for features you'd like us to prioritize.
@iamkarlson If you just want it to not throw for empty collections please file a new issue.

All 18 comments

5 figures :trollface:

This was completely not on purpose. It just happens to be that I had to file 5 issues today. :trollface:

I think this is the first repo in the 'aspnet' org with this number of issues. Do we celebrate?

Is there really no workaround till you're busy implementing?

@iamkarlson The workaround is to specify the foreign key values. See https://docs.microsoft.com/en-us/ef/core/modeling/data-seeding

@AndriySvyryd Thanks, actually I did how you said but the problem was that even an empty list raises such error. I had to manually set it to null after my lazy loading.

@iamkarlson that sounds like a different issue. Could you file it separately and include a small repro?

Do you mind if I attach #12688 to that, just to make sure it won't be missed when you complete this task?

I still don't know what is the scenario in #12688

While both #12004 and #12688 are closed, isn't it the right time to work on this issue? Thanks in advance.

Guys, any ideas about a timeline for this feature?

It's in the backlog, so it probably will be done after 2019. Remember to vote :+1: for features you'd like us to prioritize.
@iamkarlson If you just want it to not throw for empty collections please file a new issue.

I find this issue very annoying, I have an owned entity that I want to set its property at the parent to auto-initialize if null:

```c#
class Contact
{
public int Id { get; set; }

Address _Address;
public Address Address
{
get => _Address ??= new Address { /tried this too/ ContactId = Id };
set => _Address = value;
}
}
```

When adding the parent as HasData I get the following exception:

System.InvalidOperationException: 'The seed entity for entity type 'Contact' with the key value 'Id:3' cannot be added because it has the navigation 'Address' set. To seed relationships you need to add the related entity seed to 'Address' and specify the foreign key values {'ContactId'}.'

Would love to see this functionality enabled, so we can use auto-initialized properties in entities.

Solution was adding the parent entity as anonymous class:

c# modelBuilder.Entity<Contact>().HasData(new { ... });

is there any update on this feature? Seems for me like there's not much sense in Seeding mechanism if we need to use anonymous classes for all owned properties. It's much easier to use oldfashioned manual seeding on startup, isn't it?

@Unders0n While data in the model can be useful, I would agree that manual seeding on startup is often a better approach.

Any update on when you plan to add this? For configuration data that should be only changed with the source code having data seeding is very useful, but having to depend on anonymous types is error prone and less readable

@ggonzalez94 This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 5.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources.

Was this page helpful?
0 / 5 - 0 ratings