Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

entity framework - EF Core can't include children in parent

I'm quite new to EF Core.

In my DB Context:

// STEP
modelBuilder.Entity<CorsoStepS>().HasKey(x => new { x.codCorso, x.codStep });
modelBuilder.Entity<CorsoStepS>()
    .HasMany(step => step.CorsoStepLezioni)
    .WithOne(lez => lez.CorsoStep)
    .HasForeignKey(lez => new { lez.codCorso, lez.codStep });
modelBuilder.Entity<CorsoStepS>().Ignore(step => step.CorsoStepLezioni);
////

// LEZIONI
modelBuilder.Entity<CorsoStepLezione>().HasKey(x => new { x.codCorso, x.codStep, x.codLezione });
modelBuilder.Entity<CorsoStepLezione>()
    .HasMany(lez => lez.Sessioni)
    .WithOne(sess => sess.Lezione)
    .HasForeignKey(sess => new { sess.codCorso, sess.codStep, sess.codLezione });
modelBuilder.Entity<CorsoStepLezione>().Ignore(lez => lez.Sessioni);
////

// SESSIONI
modelBuilder.Entity<CorsoStepLezioneSessione>().HasKey(x => new { x.codCorso, x.codStep, x.codLezione, x.codSessione });
modelBuilder.Entity<CorsoStepLezioneSessione>()
    .HasMany(sess => sess.Iscrizioni)
    .WithOne(iscr => iscr.Sessione)
    .HasForeignKey(iscr => new { iscr.CodCorso, iscr.CodStep, iscr.CodLezione, iscr.CodSessione });
modelBuilder.Entity<CorsoStepLezioneSessione>().Ignore(sess => sess.Iscrizioni);
////

My entities:

public class CorsoStepS
    {
        public int codCorso { get; set; }
        public int codStep { get; set; }
        public string nome { get; set; }
        public int maxPartecipanti { get; set; }
        public int order { get; set; }

        public virtual Corso Corso { get; set; }
        public virtual ICollection<CorsoStepLezione> CorsoStepLezioni { get; set; }
    }

public class CorsoStepLezione
    {
        public int codCorso { get; set; }
        public int codStep { get; set; }
        public int codLezione { get; set; }
        public string nome { get; set; }


        public CorsoStepS CorsoStep { get; set; }
        public virtual ICollection<CorsoStepLezioneSessione> Sessioni { get; set; }
    }

public class CorsoStepLezioneSessione
    {
        public int codCorso { get; set; }
        public int codStep { get; set; }
        public int codLezione { get; set; }
        public int codSessione { get; set; }
        public DateTime? data { get; set; }
        public string ora { get; set; }
        public int maxPartecipanti { get; set; }

        public virtual CorsoStepLezione Lezione { get; set; }
        public virtual ICollection<CorsoStepLezioniSessioniIscrizione> Iscrizioni { get; set; }
    }

When I call:

var lezioniCorso = _clienteContext.CorsoStepLezioni
                .Include(lezione => lezione.Sessioni);

it gives me: The expression 'lezione.Sessioni' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'.

But if I call:

var lezioniCorso = _clienteContext.CorsoStepLezioni
                .Include(lezione => lezione.CorsoStep);

it's ok.

what am I doing wrong? I'm going stupid

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is because of this line

modelBuilder.Entity<CorsoStepLezione>().Ignore(lez => lez.Sessioni);

First you are telling EF to build up the relationship and then immediately ignore it again so EF acts as if this property does not exist.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...