Make Entity Query By ValueGenerated Prop translate to computed value SQL

  Kiến thức lập trình

How can i make an EFCORE Entity ValueGenerated Property value be used when querying using linq query with .where(), instead of the database value.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.valuegeneration.valuegenerator?view=efcore-8.0

Ex: instead of directly translating from linq to sql with

(canApprove == true)

it should use in:

((Approved == null && Configuration.MaxNumber > 1) == true). 

Since is the value generator logic in the class

public class Event
{
  public Configuration Configuration {get; set;}
  public bool CanApprove
  {
        get => canApprove();
        private set => _canApprove = value;
  }

  protected virtual bool canApprove() => Approved == null && Configuration.MaxNumber > 1;
}

builder.Property(e => e.CanApprove)
  .HasValueGenerator<ValueGeneratorEventCanApprove >()
  .UsePropertyAccessMode(PropertyAccessMode.Property);`

class ValueGeneratorEventCanApprove: Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator
{
   public override bool GeneratesTemporaryValues => false;
   protected override object NextValue(EntityEntry entry) => ((Event)entry.Entity).CanApprove;
}

_context.Events.Where((e) => e.CanApprove == true)
This shoud be translated to sql as:

((Approved == null && Configuration.MaxNumber > 1) == true)

instead of

`(canApprove == true)`

I cant find any solution different to try unless filter by using the background fields used in the logic of .canApprove.

var query = context.Events
    .Where(e => e.Approved == null && e.Configuration.MaxNumber > 1)
    .Select(e => new
    {
        Event = e,
        CanApprove = e.Approved == null && e.Configuration.MaxNumber > 1
    })
    .Where(e => e.CanApprove)
    .Select(e => e.Event);

Or

_context.Events.Where((e) => e.Approved == true && e.Configuration.MaxNumber> 1)

Boath approaches are not good for maintanable purposes since canApprove logic is quite complex, and used in many places.

New contributor

aRTHUR is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

Make Entity Query By ValueGenerated Prop translate to computed value SQL

How can i make an EFCORE Entity ValueGenerated Property value be used when querying using linq query with .where(), instead of the database value.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.valuegeneration.valuegenerator?view=efcore-8.0

Ex: instead of directly translating from linq to sql with

(canApprove == true)

it should use in:

((Approved == null && Configuration.MaxNumber > 1) == true). 

Since is the value generator logic in the class

public class Event
{
  public Configuration Configuration {get; set;}
  public bool CanApprove
  {
        get => canApprove();
        private set => _canApprove = value;
  }

  protected virtual bool canApprove() => Approved == null && Configuration.MaxNumber > 1;
}

builder.Property(e => e.CanApprove)
  .HasValueGenerator<ValueGeneratorEventCanApprove >()
  .UsePropertyAccessMode(PropertyAccessMode.Property);`

class ValueGeneratorEventCanApprove: Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator
{
   public override bool GeneratesTemporaryValues => false;
   protected override object NextValue(EntityEntry entry) => ((Event)entry.Entity).CanApprove;
}

_context.Events.Where((e) => e.CanApprove == true)
This shoud be translated to sql as:

((Approved == null && Configuration.MaxNumber > 1) == true)

instead of

`(canApprove == true)`

I cant find any solution different to try unless filter by using the background fields used in the logic of .canApprove.

var query = context.Events
    .Where(e => e.Approved == null && e.Configuration.MaxNumber > 1)
    .Select(e => new
    {
        Event = e,
        CanApprove = e.Approved == null && e.Configuration.MaxNumber > 1
    })
    .Where(e => e.CanApprove)
    .Select(e => e.Event);

Or

_context.Events.Where((e) => e.Approved == true && e.Configuration.MaxNumber> 1)

Boath approaches are not good for maintanable purposes since canApprove logic is quite complex, and used in many places.

New contributor

aRTHUR is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT