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

Data Seeding: Add support for navigations #10000

Open
Tracked by #22947
AndriySvyryd opened this issue Oct 6, 2017 · 21 comments
Open
Tracked by #22947

Data Seeding: Add support for navigations #10000

AndriySvyryd opened this issue Oct 6, 2017 · 21 comments

Comments

@AndriySvyryd
Copy link
Member

This depends on #9914

@bricelam bricelam mentioned this issue Oct 6, 2017
10 tasks
@smitpatel
Copy link
Contributor

5 figures :trollface:

@AndriySvyryd
Copy link
Member Author

AndriySvyryd commented Oct 6, 2017

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

@ajcvickers ajcvickers added this to the Backlog milestone Oct 11, 2017
@Eilon
Copy link
Member

Eilon commented Oct 12, 2017

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

@iamkarlson
Copy link

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

@AndriySvyryd
Copy link
Member Author

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

@iamkarlson
Copy link

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

@AndriySvyryd
Copy link
Member Author

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

@iamkarlson
Copy link

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

@AndriySvyryd
Copy link
Member Author

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

@tohidazizi
Copy link

tohidazizi commented Nov 24, 2018

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

@iamkarlson
Copy link

Guys, any ideas about a timeline for this feature?

@AndriySvyryd
Copy link
Member Author

It's in the backlog, so it probably will be done after 2019. Remember to vote 👍 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.

@weitzhandler
Copy link
Contributor

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:

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.

@weitzhandler
Copy link
Contributor

Solution was adding the parent entity as anonymous class:

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

@Unders0n
Copy link

Unders0n commented Feb 1, 2020

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?

@ajcvickers
Copy link
Member

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

@ggonzalez94
Copy link

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

@ajcvickers
Copy link
Member

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

@pantonis
Copy link

Any workaround for optional relationships?

@rcollette
Copy link

rcollette commented Mar 9, 2021

Seeding Owned Entities gets even uglier if you have to implement the hack to allow composite indexes comprised of properties from both the owning and owned entity types. Now I have to seed the shadow property(ies) that were created for the indexing hack.

OwnedNavigationBuilder<SoftwareRelease, SemanticVersion> softwareReleaseNavigationBuilder = null!;
modelBuilder.Entity<SoftwareRelease>()
    .OwnsOne(p => p.SemanticVersion,
        oe =>
        {
            // Workaround for the fact that owned entities cannot currently be used in a composite key with properties of the owning entity.
            // Shadow the owning entity property into the owned entity.
            // https://github.com/dotnet/efcore/issues/11336
            oe.Property<string>("ShadowProductDeliveryGroupCode")
                .HasColumnName("product_delivery_group_code");
            oe.HasIndex("ShadowProductDeliveryGroupCode", "Major", "Minor", "Patch");
            softwareReleaseNavigationBuilder = oe;
        });

OwnedNavigationBuilder later used to seed data.

private static void SeedSoftwareReleaseVersions(OwnedNavigationBuilder<SoftwareRelease,SemanticVersion> builder)
{
    builder
        .HasData(
            // When seeding Owned entities, we have to use an anonymous type because the primary key field is a shadow field
            CreateSoftwareReleaseVersion(1, "6.0.17", "code1"),
            CreateSoftwareReleaseVersion(2, "6.0.18", "code1"),
            CreateSoftwareReleaseVersion(3, "9.9.5", "code2"),
            CreateSoftwareReleaseVersion(4, "9.9.6", "code2"),
            CreateSoftwareReleaseVersion(5, "9.9.7", "code2"),
            CreateSoftwareReleaseVersion(6, "9.9.8", "code2")
        );
}

private static dynamic CreateSoftwareReleaseVersion(long softwareReleaseId, string versionDisplay,string shadowProductDeliveryGroupCode)
{
    SemanticVersion version = new(versionDisplay);
    return new
    {
        version.Major,
        version.Minor,
        version.Patch,
        SoftwareReleaseId = softwareReleaseId,
        version.Display,
        ShadowProductDeliveryGroupCode = shadowProductDeliveryGroupCode
    };
}

References:
#11336
#12004
https://docs.microsoft.com/en-us/ef/core/modeling/data-seeding
https://docs.microsoft.com/en-us/archive/msdn-magazine/2018/august/data-points-deep-dive-into-ef-core-hasdata-seeding

@hkusulja
Copy link

hkusulja commented Sep 4, 2024

So many github issues linked to this, and depend on this.
It passed so many years.
It is still tagged as consider for current release.

Any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests