Reputation: 11
My functional tests use Microsoft.EntityFrameworkCore.InMemory version 7.0.20.
When I run the functional tests for any entity that has a dateonly field, I get an error:
Expression of type 'System.Nullable`1[System.DateOnly]' cannot be used for parameter of type 'System.DateTime' of method 'System.DateOnly FromDateTime(System.DateTime)' (Parameter 'arg0')
I use this DateOnlyConverter
:
public class DateOnlyConverter : ValueConverter<DateOnly, DateTime>
{
/// <summary>
/// Creates a new instance of this converter.
/// </summary>
public DateOnlyConverter() : base(
d => d.ToDateTime(TimeOnly.MinValue),
d => DateOnly.FromDateTime(d))
{ }
}
This is the entity configuration:
public void Configure(EntityTypeBuilder<Payback> builder)
{
builder.ToTable("Paybacks").HasKey(k => k.Id);
// other properties
builder
.OwnsOne(p => p.CurrentPaybackSchedule, s =>
{
s.WithOwner();
s.Property(s => s.ScheduleDate).HasConversion<DateOnlyConverter>().HasColumnName(nameof(Payback.CurrentPaybackSchedule.ScheduleDate));
s.HasIndex(p => p.ScheduleDate);
});
}
Entity
public class Payback : BaseEntity<Guid>, IAggregateRoot
{
//other properties
public PaybackSchedule CurrentPaybackSchedule { get; private set;}
public Payback()
{
//ef
}
}
public class PaybackSchedule : ValueObject
{
public DateOnly ScheduleDate { get; init; }
public PaybackScheduleStatus Status { get; private set; }
public DateTimeOffset TimeEntered { get; private set; }
public PaybackSchedule(DateOnly scheduleDate, PaybackScheduleStatus status)
{
ScheduleDate = Guard.Against.Default(scheduleDate, nameof(scheduleDate));
Status = status;
TimeEntered = DateTimeOffset.Now;
}
public PaybackSchedule()
{
//ef
}
public void UpdatePaybackScheduleStatus(PaybackScheduleStatus updatedStatus)
{
Status = updatedStatus;
}
public PaybackSchedule CloneWithRescheduledDate(DateOnly newScheduleDate)
{
return new PaybackSchedule(newScheduleDate, Status);
}
}
I am not sure why the in memory database is pulling the field as a dateonly.
I have checked the versioning of all my EF Core packages and they all seem to be at v7.0.20.
When I run the query outside of the functional tests the code works.
Upvotes: 1
Views: 47