Search code examples
c#entity-framework-core.net-core-3.0ef-core-3.0

What is alternative for HasForeignKey and ForSqlServerUseSequenceHiLo method in ef core3?


In a project with .net core 2.2 and ef core 2.2 I had below codes In my dbContext to set one to many relationship between Reference Entity and it,s workdowns in OnModelCreating method of dbContext:

 modelBuilder.Entity<Reference>().OwnsMany<WorkDown>("WorkDowns", a =>
        {
            a.HasForeignKey("ReferenceId");
            a.Property(ca => ca.Action).IsRequired();
            a.Property(ca => ca.Note);
            a.Property(ca => ca.WorkDownId).ForSqlServerUseSequenceHiLo("WorkDownSequence");
            a.HasKey("WorkDownId");
        });

It works perfectly in that project wit .netcor 2.2, but in another project that I use .net core and ef core 3 I could not find any alternativ for HasForeignKey and ForSqlServerUseSequenceHiLo extension methods is there any substitue method or any alternative way in efcore 3 ?


Solution

  • This is one of the 3.0 breaking changes - Configuration API for owned type relationships has changed:

    The configuration related to the relationship between owner and owned should now be chained after WithOwner() similarly to how other relationships are configured. While the configuration for the owned type itself would still be chained after OwnsOne()/OwnsMany().

    i.e. you need to use one of the WithOwner overloads in order to get access to relationship configuration methods like HasForeignKey / HasPrincipalKey, e.g.

    a.WithOwner()
        .HasForeignKey("ReferenceId");