[英]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.
我将 Entity Framework 6 与 .NET Core 2.2 一起使用。
I have Driver
class which have list of many DailyRoute
:我有
Driver
class ,其中列出了许多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:这是
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.问题是我调试程序,当我更新用户然后显示实际的
dbcontext
元素时,有带有 dailyRoute 列表的驱动程序,但是在下一个请求时,当我尝试显示所有驱动程序时,我得到了带有空每日路由列表的驱动程序。 In the database, I have a table with dailyRoute with driver Id and this Id does exist in the Driver
table.在数据库中,我有一张带有驱动程序 ID 的 dailyRoute 的表,并且该 ID 确实存在于
Driver
表中。
The dbContext
class: 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;这是 class 当我尝试获取驱动程序时,驱动程序存在但dailyRoute列表为空;
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: Driver
表:
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.
您可以通过在 linq 查询中添加 Include function 来做到这一点。 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.