Sto migrando una libreria Entity Framework 6.1.3 Prima libreria a Entity Framework Core con C # e .NET Framework 4.7.
Ho cercato su Entity Framework Core con Google ma non ho trovato molte informazioni a riguardo, quindi ho provato a farlo da solo.
In Entity Framework 6.1.3 ho questa classe di configurazione:
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);
}
}
}
Sono migrato a questo:
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);
}
}
}
Ma il costruttore non ha il metodo HasRequired
e penso che gli altri metodi WithOptional
, WithMany
e WillCascadeOnDelete
.
Sono migrato a questo, ma non sono sicuro se sia corretto:
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);
}
Ho controllato la documentazione di EntityTypeBuilder ma non so quali metodi devo usare al suo posto o se questo è il modo giusto per migrare a Entity Framework Core.
Questa relazione non è uno-a-zero:
builder.HasOne(ag_ch => ag_ch.Code)
.WithOne(c => c.AggregationChild)
.OnDelete(DeleteBehavior.SetNull);
In questa risposta SO ha detto che devo mettere ForeignKey a null lo imposterà come opzionale, ma non posso impostare Code.CodeId
come nullable.
L'installazione di EF6 sta creando una cosiddetta associazione di chiavi primarie condivise one-to-one in cui il PK dell'entità dipendente è anche un FK dell'entità principale.
Le cose sono cambiate in EF Core. Supporta naturalmente entrambe le associazioni PK e FK condivise one-to-one. Anche opzionali / obbligatori non vengono utilizzati per determinare i fini principali e dipendenti dell'associazione. IsRequired
è usato per controllare se l'entità dipendente può esistere senza principio e si applica solo con FK separato. Mentre HasForeignKey
e HasPrincipalKey
vengono utilizzati per determinare l'estremità principale e dipendente dell'associazione e anche mappare l'FK dipendente e il PK principale / Chiave alternativa.
Detto questo, la configurazione EFC equivalente è la seguente:
builder.HasOne(ag_ch => ag_ch.Code)
.WithOne(c => c.AggregationChild)
.HasForeignKey<AggregationChildren>(ag_ch => ag_ch.AggregationChildrenId)
.OnDelete(DeleteBehavior.Restrict);
Quindi iniziamo con la definizione della relazione usando HasOne
+ WithOne
.
Quindi HasForeignKey<AggregationChildren>(ag_ch => ag_ch.AggregationChildrenId)
per dire a EF che (1) AggregationChildren
è la fine dipendente della relazione e (2) che PK AggregationChildrenId
dovrebbe essere usato anche come FK.
Infine, OnDelete(DeleteBehavior.Restrict)
è l'equivalente EFC di EF6 WillCascadeOnDelete(false)
. Le altre opzioni come ClientSetNull
e SetNull
applicano solo quando il dipendente ha un FK facoltativo separato, che non è il caso con l'associazione PK condivisa.
Riferimento: relazioni