[英]Dbset of in memory database returns entity with empty collection navigation property
The problem with the test:测试的问题:
[Fact]
public async Task CountOfDaysOffInCurrentYearToToday()
{
var application1 = new Domain.Entities.Application
{
Type = ApplicationType.DayOff, Status = ApplicationStatus.Approved, StatedDates =
{
new ApplicationDate { Date = new DateTime(2021, 4, 1) },
new ApplicationDate { Date = new DateTime(2022, 4, 1) },
new ApplicationDate { Date = new DateTime(2023, 4, 1) }
}
};
var application2 = new Domain.Entities.Application
{
Type = ApplicationType.DayOffAtExpenseOfVacation, Status = ApplicationStatus.Approved, StatedDates =
{
new ApplicationDate { Date = new DateTime(2021, 4, 1) },
new ApplicationDate { Date = new DateTime(2022, 4, 1) },
new ApplicationDate { Date = new DateTime(2023, 4, 1) }
}
};
var application3 = new Domain.Entities.Application
{
Type = ApplicationType.DayOffAtExpenseOfVacation, Status = ApplicationStatus.Approved, StatedDates =
{
new ApplicationDate { Date = new DateTime(2021, 4, 1) },
new ApplicationDate { Date = new DateTime(2022, 4, 1) },
new ApplicationDate { Date = new DateTime(2023, 4, 1) }
}
};
var user = new AppUser
{
ApplicantApplications = { application1, application2, application3 }
};
var context = GetDbContext();
await context.Users.AddAsync(user);
await context.SaveChangesAsync();
var bl = new UserCalendarBL(context, GetDateTime());
Assert.Equal(4, await bl.CountOfDaysOffInCurrentYearToToday(user.Id));
}
When I debug it, it shows that StatedDates
is empty in every application:当我调试它时,它显示每个应用程序中的
StatedDates
都是空的:
private static IDbContext GetDbContext()
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase(new StackFrame(1, true).GetMethod()!.Name)
.Options;
return new AppDbContext(options);
}
private IDateTimeService GetDateTime()
{
var mock = new Mock<IDateTimeService>();
mock.Setup(e => e.Now()).Returns(new DateTime(2022, 5, 1));
return mock.Object;
}
These are entity types:这些是实体类型:
public class Application
{
public int Id { get; set; }
public AppUser Applicant { get; set; }
public string ApplicantId { get; set; }
public DateTime DateOfCreation { get; set; }
public DateTime? DateOfSent { get; set; }
public ApplicationType Type { get; set; }
public ApplicationStatus Status { get; set; }
public List<ApplicationDate> StatedDates { get; set; } = new List<ApplicationDate>();
public AppUser Coordinator { get; set; }
public string CoordinatorId { get; set; }
public string Comment { get; set; }
}
public class AppUser : IdentityUser
{
public List<Application> ApplicantApplications { get; set; } = new List<Application>();
public List<Application> CoordinatorApplications { get; set; } = new List<Application>();
public string FullName { get; set; }
public string Position { get; set; }
public int? CountOfUnusedVacationDaysOverPastYears { get; set; }
}
public class ApplicationDate
{
public DateTime Date { get; set; }
public int ApplicationId { get; set; }
public Application Application { get; set; }
}
ApplicationDate
has composite key of Date
and ApplicationId
ApplicationDate
具有Date
和ApplicationId
的复合键
These are testing methods:这些是测试方法:
public async Task<int> CountOfDaysOffInCurrentYearToToday(string userId)
{
return await Task.Run(() =>
{
var applications = ApprovedApplicationsInCurrentYearToToday(userId).AsParallel().Where(
application =>
application.Type == ApplicationType.DayOff ||
application.Type == ApplicationType.DayOffAtExpenseOfVacation ||
application.Type == ApplicationType.DayOffAtExpenseOfWorkingOut).ToImmutableArray();
return applications.Sum(CountOfApplicationDatesInCurrentYearToToday);
});
}
private IEnumerable<Domain.Entities.Application> ApprovedApplicationsInCurrentYearToToday(string userId)
{
return _context.Applications
.Include(application => application.Applicant)
.AsNoTracking()
.AsParallel()
.Where(application =>
application.Applicant.Id == userId &&
GetApplicationStatedDates(application)
.Any(date => date.Year == _dateTime.Now().Year && date < _dateTime.Now()));
}
In the where method, by stopping on a breakpoint, I can verify that StatedDates
is empty.在 where 方法中,通过在断点处停止,我可以验证
StatedDates
是否为空。
You have to load the navigation property StatedDates
explicitly like this:您必须像这样显式加载导航属性
StatedDates
:
private IEnumerable<Domain.Entities.Application> ApprovedApplicationsInCurrentYearToToday(string userId)
{
return _context.Applications
.Include(application => application.Applicant)
.Include(application => application.StatedDates)
.AsNoTracking()
.AsParallel()
.Where(application =>
application.Applicant.Id == userId &&
GetApplicationStatedDates(application)
.Any(date => date.Year == _dateTime.Now().Year && date < _dateTime.Now()));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.