Sto provando Entity Framework Core con PostgreSQL. Ho due tavoli con una relazione uno a molti. Secondo il rapporto, il film ha un anno e l'anno ha molti film. Ma una volta recuperati i film, l'anno non contiene, l'anno in ciascun film è nullo a meno che non si recuperino gli anni. Ma dopo aver recuperato gli anni, i film stanno diventando con gli anni. Quando passo l'anno recuperando la riga di codice in alto. È successo lo stesso Gli anni non hanno i film se non il recupero dei film. Quindi, dopo aver recuperato i filmati, gli anni selezionati diventano i film. Qualcuno sa come risolverlo? È come un carico pigro.
Ecco le mie due entità.
public class T_MOVIE
{
[Key]
public long Oid { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime CreatedDate { get; set; }
public long? YearOid { get; set; }
[ForeignKey("YearOid")]
public T_YEAR MOVIE_YEAR { get; set; }
}
public class T_YEAR
{
public T_YEAR()
{
this.YEAR_MOVIEs = new HashSet<DataModel.T_MOVIE>();
}
[Key]
public long Oid { get; set; }
public string Name { get; set; }
public ICollection<T_MOVIE> YEAR_MOVIEs { get; set; }
}
E in OnModelCreating,
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<T_MOVIE>().HasKey(m => m.Oid);
builder.Entity<T_MOVIE>().HasOne(m => m.MOVIE_YEAR).WithMany(m => m.YEAR_MOVIEs);
builder.Entity<T_YEAR>().HasKey(m => m.Oid);
builder.Entity<T_YEAR>().HasMany(m => m.YEAR_MOVIEs).WithOne(m => m.MOVIE_YEAR);
}
Quindi, recupero i dati.
var movies = _db.T_MOVIE.ToList();
var years = _db.T_YEAR.ToList();
Grazie.
Devi aggiungere "Include ()" nella tua query. A meno che non lo faccia, restituisce relazioni nulle.