简体   繁体   中英

Entity Framework 6 on .NET Core 2.2 - one to many return empty list

I spend all day to repair this bug but nothing happened. I use Entity Framework 6 with .NET Core 2.2.

I have Driver class which have list of many DailyRoute :

public class Driver
{
    [Key]
    public Guid DriverId { get; protected set; }
    public Vehicle Vehicle { get; protected set; }

    private ISet<DailyRoute> _dailyRoutes = new HashSet<DailyRoute>();

    public virtual ICollection<DailyRoute> DailyRoutes
    {
        get => _dailyRoutes;

        set => _dailyRoutes = new HashSet<DailyRoute>(value);
    }

    public DateTime UpdatedAt { get; private set; }

    protected Driver() 
    {
    }

    public Driver (Guid userid)
    {
        DriverId = userid;
    }
}

This is the DailyRoute class:

public class DailyRoute
{
    public Guid Id { get; set; }
    public DateTime DateTime { get; protected set; }

    private ISet<PassengerNode> _passengerNodes = new HashSet<PassengerNode>();

    public Route Route { get; protected set; }

    public Driver Driver { get; set; }

    public ICollection<PassengerNode> PassengerNodes => _passengerNodes;

    public DailyRoute()
    {
    }

    protected DailyRoute(DateTime dateTime, Route route, Guid id)
    {
        Id = id;
        Route = route;
        DateTime = dateTime;
    }
}

The problem is I debug program and when I update user and then show the actual dbcontext element there was driver with the dailyRoute list but and on the next request when I try to show all drivers, I got the driver with empty daily route list. In the database, I have a table with dailyRoute with driver Id and this Id does exist in the Driver table.

The dbContext class:

public class PassengerContext : DbContext
{
    public PassengerContext(DbContextOptions<PassengerContext> options): base(options)
    {
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Driver> Drivers { get; set; }
    public DbSet<RefreshToken> RefreshTokens { get; set; }
    public DbSet<DailyRoute> DailyRoutes { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Driver>()
            .HasMany(e => e.DailyRoutes).WithOne(k => k.Driver).IsRequired();
    }
}

This is the class when i try to get Driver, the driver exist but the dailyRoute list is empty;

public class DriverRepository : IDriverRepository
{
    private readonly PassengerContext _passengerContext;

    public DriverRepository(PassengerContext passengerContext)
    {
        _passengerContext = passengerContext;
    }

    public async Task<Driver> GetAsync(Guid userId)
    {
        return await _passengerContext.Drivers.SingleOrDefaultAsync(x => x.DriverId == userId);
    }

Driver table:

司机表

DailyRoute table:

DailyRoute 表

There are different loading policies in entity framework such as "eager loading" and "lazy loading". By default it would be lazy loading.

If you want the associated entities to loaded, then you have to make it eager loading. you can do this by adding a Include function in your linq query. The Link could probably help you.

The below change should probably fix the issue for you. Not compiled, so minor compilations issues possible

 return await _passengerContext.Drivers.Include(x => x.DailyRoutes).SingleOrDefaultAsync(x => x.DriverId == userId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM