efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Managed model data (HasData): Add support for navigations

Open AndriySvyryd opened this issue 8 years ago • 21 comments

This depends on #9914

AndriySvyryd avatar Oct 06 '17 20:10 AndriySvyryd

5 figures :trollface:

smitpatel avatar Oct 06 '17 20:10 smitpatel

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

AndriySvyryd avatar Oct 06 '17 20:10 AndriySvyryd

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

Eilon avatar Oct 12 '17 16:10 Eilon

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

iamkarlson avatar Jul 16 '18 16:07 iamkarlson

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

AndriySvyryd avatar Jul 16 '18 17:07 AndriySvyryd

@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 avatar Jul 17 '18 07:07 iamkarlson

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

AndriySvyryd avatar Jul 17 '18 19:07 AndriySvyryd

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

iamkarlson avatar Aug 15 '18 10:08 iamkarlson

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

AndriySvyryd avatar Aug 15 '18 18:08 AndriySvyryd

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

tohidazizi avatar Nov 24 '18 03:11 tohidazizi

Guys, any ideas about a timeline for this feature?

iamkarlson avatar Dec 27 '18 21:12 iamkarlson

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.

AndriySvyryd avatar Dec 28 '18 03:12 AndriySvyryd

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 avatar Dec 02 '19 16:12 weitzhandler

Solution was adding the parent entity as anonymous class:

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

weitzhandler avatar Dec 02 '19 16:12 weitzhandler

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 avatar Feb 01 '20 08:02 Unders0n

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

ajcvickers avatar Feb 03 '20 22:02 ajcvickers

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 avatar Mar 17 '20 13:03 ggonzalez94

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

ajcvickers avatar Mar 17 '20 15:03 ajcvickers

Any workaround for optional relationships?

pantonis avatar Jan 30 '21 10:01 pantonis

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

rcollette avatar Mar 09 '21 18:03 rcollette

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?

hkusulja avatar Sep 04 '24 14:09 hkusulja