EntityFrameworkCore.Projectables icon indicating copy to clipboard operation
EntityFrameworkCore.Projectables copied to clipboard

When using alongside a modelBuilder.HasDbFunction() throws `Non-static method requires a target`

Open douglasg14b opened this issue 1 year ago • 2 comments

I added a DB fucntion:

        modelBuilder.HasDbFunction(
                typeof(DateTimeExtensions).GetMethod(nameof(DateTimeExtensions.MonthsDeltaFractionalDb),
                    new[] { typeof(DateTimeOffset), typeof(DateTimeOffset) })!)
            .HasName("months_delta_fractional");

And am using it in a query:

        BudgetDetailsResponse? budget = await _dbContext.Companies
            .Include(x => x.Orders)
            .Include(x => x.Campaigns)
            .Where(x => x.Id == companyId)
            .Select(x => new BudgetDetailsResponse
            {
                UsedBudget = x.Orders!
                    .ExcludeOptionalOrder(excludeOrderId)
                    .Where(order => Order.UsedBudgetStatuses.Contains(order.Status))
                    .Sum(order => order.TotalPrice / order.FlightTimeMonths),
            }).FirstOrDefaultAsync();

With an entity configured as so:

    public double FlightTimeMonths => DateTimeExtensions.MonthsDeltaFractionalDb(StartDate, EndDate);

    [Projectable]
    public int FlightTimeDays => (EndDate - StartDate).Days;

    [Projectable]
    public double AvgPlaysPerDay => FlightTimeMonths * PlaysPerMonth / FlightTimeDays;

    [Projectable]
    public double TotalPlays => AvgPlaysPerDay * FlightTimeDays;

    [Projectable]
    public double TotalPrice => Pricing.GetTotalPrice((int)Math.Round(TotalPlays));

It seems that the use of a DBFunction and a projectable together creates a problem?

douglasg14b avatar Nov 21 '24 04:11 douglasg14b

Can you clarify what is not working?

https://github.com/koenbeuk/EntityFrameworkCore.Projectables/blob/issue/123/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.cs translates just fine.

koenbeuk avatar Dec 07 '24 19:12 koenbeuk

thanks for the response!

When I use the above, queries throw the exception:

Non-static method requires a target

If I remove the HasDbFunction, stop projecting, or stop using projectable proeprties, the problem goes away.

I'll see if I can find the differences in the setup from what you linked to. It may take me a bit to circle back around,

douglasg14b avatar Dec 07 '24 20:12 douglasg14b