Exception "Property 'System.Datetime Date' is not defined for type 'System.Nullable`1[System.Datetime]' (Parameter 'Property')"

Asked

Viewed 41 times

0

I upgraded EF Core to version 3.1.6 and am getting this exception when running a test.

The methods that cause this are these two:

public static IQueryable<CashoutRequest> WhereTransferOrInstitutionTransferStartDate
(
    this IQueryable<CashoutRequest> cashoutRequests, 
    DateTime? date
)
{
    if (date == null) return cashoutRequests;

    var startDate = date.Value.Date;

    return cashoutRequests.Where(cashoutRequest => 
        cashoutRequest.Transfer.CreatedAt.Date >= startDate
        || 
        cashoutRequest.InstitutionTransfer.CreatedAt.Date >= startDate
    );
}

public static IQueryable<CashoutRequest> WhereTransferOrInstitutionTransferEndDate
(
    this IQueryable<CashoutRequest> cashoutRequests, 
    DateTime? date
)
{
    if (date == null) return cashoutRequests;

    var endDate = date.Value.Date;

    return cashoutRequests.Where(cashoutRequest => 
        cashoutRequest.Transfer.CreatedAt.Date <= endDate
        || 
        cashoutRequest.InstitutionTransfer.CreatedAt.Date <= endDate
    );
}

The fields CreatedAt are not nullables, as if the Entity Framework Core made them nullables in the execution?

Entities:

    public class CashoutRequest
        {
            public long Id { get; set; }
            public long? TransferId { get; set; }
            public DateTime CreatedAt { get; set; } = DateTime.Now;
            public DateTime? ReprovedAt { get; set; }
            public DateTime? ApprovedAt { get; set; }
            public DateTime? CanceledAt { get; set; }
            public Transfer Transfer { get; set; }
            public InstitutionTransfer InstitutionTransfer { get; set; }
}

public class Transfer
    {
        public long Id { get; set; }
        public DateTime CreatedAt { get; set; } = DateTime.Now;
        public DateTime? ApprovedAt { get; set; }
        public DateTime? CanceledAt { get; set; }
        public DateTime? ReprovedAt { get; set; }
    }

public class InstitutionTransfer
{
    public long Id { get; set; }
    public DateTime CreatedAt { get; set; } = DateTime.Now;
    public DateTime? ApprovedAt { get; set; }
    public DateTime? CanceledAt { get; set; }
    public DateTime? ReprovedAt { get; set; }
    public long? CashoutRequestId { get; set; }

    public CashoutRequest CashoutRequest { get; set; }
}
  • Was working and stopped working after the update? Post also the entity CashoutRequest, Transfer and InstitutionTransfer?

  • Yes, it stopped working after the update

  • @novic Entities are pretty big. I have tested now by removing the Date and it worked. Now I will search how to do to buy only the date

  • Which bank is? and has the EF method (https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.ef.functions?view=efcore-5.0) and https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbfunctions?view=efcore-5.0

  • maybe: https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.sqlserverdbfunctionsextensions.datefromparts?view=efcore-5.0#Microsoft_EntityFrameworkCore_SqlServerDbFunctionsExtensions_DateFromParts_Microsoft_EntityFrameworkCore_DbFunctions_System_Int32_System_Int32_System_Int32_

  • @novic Is SQL Server

  • Without the entities it’s hard to say!

  • 1

    @novic I put the entities

  • must be the nullable of the vestments? make the change DateTime date for DateTime date will work out then you have to use another strategy.

  • because so startDate this different from CreatedAt, respectively Nullables and Datetime ... are different

  • Another thing this method is not needed that way! see if you have a Linq Builder construct just copy the last change and move on ...

  • The problem is not the method parameter. I changed the Date property to Year and the exception was the same, changing only the message, referencing the Year property

  • is yes guy are different, test with date without being nullable

  • but you noticed that DateTime? date and you’re using this variable for comparisons?

  • @Leandroangelo But I pass the date to startDate by accessing Value, so it is a Datetime When I took the . Date the test passed

Show 10 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.