12

I'm migrating a Entity Framework 6.1.3 Code First library to Entity Framework Core with C# and .NET Framework 4.7.

I've been searching about Entity Framework Core with Google but I haven't found many information about it so I have try to do it by myself.

On Entity Framework 6.1.3 I have this configuration class:

using System.Data.Entity.ModelConfiguration;

namespace MyProject.Data.SqlServer.Configurations
{
    class AggregationChildrenConfiguration : EntityTypeConfiguration<AggregationChildren>
    {
        public AggregationChildrenConfiguration()
        {
            HasKey(ag_ch => ag_ch.AggregationChildrenId);

            HasRequired(ag_ch => ag_ch.Aggregation)
                .WithMany(ag => ag.AggregationChildren)
                .HasForeignKey(ag_ch => ag_ch.AggregationId);

            HasRequired(ag_ch => ag_ch.Code)
                .WithOptional(c => c.AggregationChild)
                .WillCascadeOnDelete(false);
        }
    }
}

I have migrated to this one:

using DataLibrary;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace BusinessLibrary.Configurations
{
    class AggregationChildrenConfiguration : IEntityTypeConfiguration<AggregationChildren>
    {
        public void Configure(EntityTypeBuilder<AggregationChildren> builder)
        {
            builder.HasKey(ag_ch => ag_ch.AggregationChildrenId);

            builder.HasRequired(ag_ch => ag_ch.Aggregation)
                .WithMany(ag => ag.AggregationChildren)
                .HasForeignKey(ag_ch => ag_ch.AggregationId);

            builder.HasRequired(ag_ch => ag_ch.Code)
                .WithOptional(c => c.AggregationChild)
                .WillCascadeOnDelete(false);
        }
    }
}

But builder hasn't got HasRequired method, and I think the others methods WithOptional, WithMany and WillCascadeOnDelete either.

I have migrated to this, but I'm not sure if it is correct:

using DataLibrary;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;

namespace BusinessLibrary.Configurations
{
    class AggregationChildrenConfiguration : IEntityTypeConfiguration<AggregationChildren>
    {
        public void Configure(EntityTypeBuilder<AggregationChildren> builder)
        {
            builder.HasKey(ag_ch => ag_ch.AggregationChildrenId);

            builder.HasOne(ag_ch => ag_ch.Aggregation)
                .WithMany(ag => ag.AggregationChildren)
                .HasForeignKey(ag_ch => ag_ch.AggregationId)
                .IsRequired();

            builder.HasOne(ag_ch => ag_ch.Code)
                .WithOne(c => c.AggregationChild)
                .OnDelete(DeleteBehavior.SetNull);
        }

I've been checking EntityTypeBuilder documentation but I don't know which methods do I have to use instead or if this is the right way to migrate to Entity Framework Core.

This relationship is not one-to-zero:

builder.HasOne(ag_ch => ag_ch.Code)
    .WithOne(c => c.AggregationChild)
    .OnDelete(DeleteBehavior.SetNull);

In this SO Answer said that I have to put ForeignKey to null it will set it as optional, but I can't set Code.CodeId as nullable.

1 Answer 1

23

The EF6 setup is creating a so called One-To-One Shared Primary Key Association where the PK of the dependent entity is also a FK to principal entity.

The things have changed in EF Core. It supports naturally both shared PK and FK one-to-one associations. Also optional/required are not used to determine the principal and dependent ends of the association. IsRequired is used to control if the dependent entity can exists w/o principal and applies only whith separate FK. While HasForeignKey and HasPrincipalKey are used to determine the principal and dependent ends of the association and also map the dependent FK and principal PK / Alternate Key.

With that being said, the equivalent EFC configuration is as follows:

builder.HasOne(ag_ch => ag_ch.Code)
    .WithOne(c => c.AggregationChild)
    .HasForeignKey<AggregationChildren>(ag_ch => ag_ch.AggregationChildrenId)
    .OnDelete(DeleteBehavior.Restrict);

So we start with defining the relationship using HasOne + WithOne.

Then HasForeignKey<AggregationChildren>(ag_ch => ag_ch.AggregationChildrenId) to tell EF that (1) AggregationChildren is the dependent end of the relationship, and (2) that the PK AggregationChildrenId should also be used as FK.

Finally, OnDelete(DeleteBehavior.Restrict) is the EFC equivalent of the EF6 WillCascadeOnDelete(false). The other options like ClientSetNull and SetNull apply only when the dependent has a separate optional FK, which is not the case with shared PK association.

Reference: Relationships

4
  • Thanks for your answer. There is something that I haven't understood: where is you telling that the relationship from Code to AggregationChildren is optional?
    – VansFannel
    Sep 27, 2017 at 10:42
  • 2
    There is no need in this case. Only dependent entity with separate FK may have optional or required. See the description on IsRequired method - "Configures whether this is a required relationship (i.e. whether the foreign key property(s) can be assigned null).". EF6 use optional/required terms for different purpose. In EFC by default relationships are optional except you have explicit separate non nullable FK property.
    – Ivan Stoev
    Sep 27, 2017 at 10:50
  • In my case, this simply doesn't work. I have tried to define HasForeignKey on both the parent and the child, and it always ends up on the parent. I have tried specifying the child as the generic type, and the parent as the generic type, and it still always ends up on the parent. Dec 31, 2018 at 10:17
  • 1
    @ChaimEliyah No way. Apparently you are doing something wrong. First, there must be only 1 definition of the relationship (in order to be sure you are not creating conflicting relationship configuration). Second, the FK must be defined for child(dependent) entity. That's it. Parent(principal) never has as FK.
    – Ivan Stoev
    Dec 31, 2018 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.