EfCore OwnsOne seme nullable oggetti in mancanza di

0

Domanda

Voglio seme dati con EfCore a mio Entità di Proprietà che può essere nullable

Soggetti:

public class RootEntity
{
    protected RootEntity() { }

    public Guid Id { get; set; }

    public OwnedEntityLevel1? OwnedEntityLevel1 { get; set; } // can be nullable
}

public class OwnedEntityLevel1
{
    public Guid Id { get; set; }
}

Modello di configurazione per DbContext:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<RootEntity>(b =>
    {
        b.OwnsOne(x => x.OwnedEntityLevel1, ob =>
        {
            ob.HasData(RootEntity.All.Select(x => new
                { x.OwnedEntityLevel1?.Id, RootEntityId = x.Id }));
        });
        b.HasData(RootEntity.All.Select(x => new { x.Id }));
    });
}

Quando cerco di creare il mio migrazione con:

dotnet ef migrations add Initial --context NullableObjectDbContext -o Migrations/NullableObject

ricevo l'errore:

Il seme di entità di entità di tipo 'OwnedEntityLevel1' non può essere aggiunto perché nessun valore è stato fornito per la proprietà required 'Id'.

Il messaggio oviously è corretto. Ma non capisco se si potesse seme nullable oggetti con .HasData in qualche modo?

I dati che sto cercando di seme:

public static RootEntity PredefinedEntity11 { get; } =
    new(
        Guid.Parse("96e1d442-bdd0-4c6f-9d01-624b27abbac3"),
        new OwnedEntityLevel1
        {
            Id = Guid.Parse("8f8eea73-0b43-412a-b0aa-a9338db6e067")
        }
    );

public static RootEntity PredefinedEntity12 { get; } =
    new(
        Guid.Parse("aae51dac-016e-472e-ad51-2f09f8cb9fbb"),
        null! // When i add this the migration fails with The seed entity for entity type 'OwnedEntityLevel1' cannot be added because no value was provided for the required property 'Id'
    );

public static IReadOnlyList<RootEntity> All { get; } =
    new List<RootEntity>(new[] { PredefinedEntity11, PredefinedEntity12 }).AsReadOnly();

Io il mio normale flusso del programma, è possibile aggiungere gli oggetti nullable senza un problema:

var ctx = new NullableObjectDbContext();
var rootEntity = new RootEntity(Guid.NewGuid(), null);
ctx.Add(rootEntity);
ctx.SaveChanges();

Ho creato una minima riproducibile esempio qui: https://github.com/enterprisebug/EfCoreHasDataNestedOwnedTypes/tree/main/EfCoreHasDataNestedOwnedTypes/NullableObject

ef-code-first entity-framework-core
2021-11-23 07:30:51
1

Migliore risposta

2

Modello di dati di semina con tipi anonimi partite di proprietà sia il nome e il tipo.

Nel tuo caso, anche se la semina tipo ha una proprietà chiamata Idil tipo è diverso dal tipo di Id proprietà del seeding entità (Nullable<Guid> dedotto da ?. operatore vs Guid), quindi non è mappata e viene generato un messaggio di errore confusione.

new
{ 
    x.OwnedEntityLevel1?.Id, // Guid? Id
    RootEntityId = x.Id      // Guid RootEntityId 
}

La soluzione di creare e popolare un Guid Id proprietà del tipo anonimo, prima di filtrare null gli oggetti, ad esempio (null perdonare operatore viene utilizzato per sopprimere la NRT avviso):

ob.HasData(RootEntity.All
    .Where(x => x.OwnedEntityLevel1 != null)
    .Select(x => new
    { 
        x.OwnedEntityLevel1!.Id, // Guid Id
        RootEntityId = x.Id      // Guid RootEntityId
    }));
2021-11-23 08:43:17

Grazie! Che risolto il mio problema. Grazie per la spiegazione, come pure! Per filtrare null valori super semplice soluzione. Non ho mai pensato! Molto apprezzato il tuo contributo!
Daniel

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................