One-to-zero Relationships with Entity Framework Core 2.0

I am migrating Entity Framework 6.1.3. The first code library in the core Entity Framework with C # and .NET Framework 4.7.

I searched about the Entity Framework Core with Google, but I did not find much information about this, so I tried to do it myself.

In 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 went to this:

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 the builder is no method HasRequired, and I think that other methods WithOptional, WithManyand WillCascadeOnDeletealso.

I switched to this, but I'm not sure if this 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 checked the EntityTypeBuilder documentation , but I don’t know what methods I need to use or if this is the right way to migrate to the Entity Framework Core.

This ratio is not zero:

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

SO- , ForeignKey null, , Code.CodeId nullable.

+4
1

EF6 " " , FK .

EF Core . , "--". / . IsRequired , FK. HasForeignKey HasPrincipalKey , FK PK/ .

EFC :

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

, HasOne + WithOne.

HasForeignKey<AggregationChildren>(ag_ch => ag_ch.AggregationChildrenId), EF, (1) AggregationChildren , (2), PK AggregationChildrenId FK.

, OnDelete(DeleteBehavior.Restrict) EFC EF6 WillCascadeOnDelete(false). , ClientSetNull SetNull, , FK, PK.

: Relationships

+4

Source: https://habr.com/ru/post/1686453/


All Articles