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
, WithMany
and WillCascadeOnDelete
also.
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.