Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

How to create a custom data filter with EF Core #4752


User avatar
0
inzone created

Hello,

I need to implement a data filter for our Event entity. The filtering should be done by Event Id, retrieved from an IEventSession we defined, and should be overriden with the UnitOfWork SetFilterParameter method.

This is what I have done so far, according to this document: <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Data-Filters">https://aspnetboilerplate.com/Pages/Doc ... ta-Filters</a>

Defined the interfaces IMayHavEvent and IMustHaveEvent Defined filter names in a static class Registered the filters at the Core project Module using Configuration.UnitOfWork.RegisterFilter

So what I need to do now, is to do the actual filtering. The docs say that EntityFramework.DynamicFilters do not apply for EF Core, so what is the most convenient way to implement the filter? If I do it at repository level, what should I override at the RepositoryBase abstract class?

Thank you!


11 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Override ShouldFilterEntity and CreateFilterExpression in your DbContext.

  • User Avatar
    0
    inzone created

    <cite>aaron: </cite> Override ShouldFilterEntity and CreateFilterExpression in your DbContext.

    Ok, I see I can get it done easily that way.

    In order to get the filter parameter defined with IUnitOfWork.SetFilterParameter, I have to use IUnitOfWork.GetFilter and check in the DataFilterConfiguration.FilterParameters collection, right?

  • User Avatar
    0
    aaron created
    Support Team

    Yes. Something like this in your DbContext:

    var eventId = CurrentUnitOfWorkProvider.Current?
                    .Filters.FirstOrDefault(f => f.FilterName == MyDataFilters.MayHaveEvent)?
                    .FilterParameters[MyDataFilters.Parameters.EventId] as long?;
    
  • User Avatar
    0
    jehadk created

    I have aspzero, can you please provide us with example to set custom filler ?

  • User Avatar
    0
    alper created
    Support Team

    see the implementation of ISoftDelete and imitate the same <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/f10fa5205c780bcc27adfe38aaae631f412eb7df/src/Abp/Domain/Entities/ISoftDelete.cs">https://github.com/aspnetboilerplate/as ... tDelete.cs</a>

  • User Avatar
    0
    jehadk created

    Sorry I set the below implementation 1)

    public interface IHasCustomer
        {
            string InsCustID { get; set; }
        }
    
    1. override ShouldFilterEntity and CreateFilterExpression as below
    protected override bool ShouldFilterEntity<TEntity>(IMutableEntityType entityType)
            {
                return base.ShouldFilterEntity<TEntity>(entityType);
            }
             protected override Expression<Func<TEntity, bool>> CreateFilterExpression<TEntity>()
            {
                Expression<Func<TEntity, bool>> expression = null;
    
                if (typeof(IHasCustomer).IsAssignableFrom(typeof(TEntity)))
                {
                                    Expression<Func<TEntity, bool>> mustHaveTenantFilter = e => ((IHasCustomer)e).InsCustID == GetCurrentCustomerIdOrNull() || (((IHasCustomer)e).InsCustID == GetCurrentCustomerIdOrNull()) == true;
                    expression = expression == null ? mustHaveTenantFilter : CombineExpressions(expression, mustHaveTenantFilter);
                }
                return base.CreateFilterExpression<TEntity>();
            }
    

    my question what the next to run and apply the filter

  • User Avatar
    0
    aaron created
    Support Team

    Tip: Wrap your code in the following for formatting and readability:

    [code]
    

    [/code:2iuqdo9h] A shortcut is to highlight your code and click on the </> button in the formatting toolbar.

  • User Avatar
    0
    jehadk created

    <cite>aaron: </cite> Tip: Wrap your code in the following for formatting and readability:

    [code]
    

    [/code:39lrdbwm] A shortcut is to highlight your code and click on the </> button in the formatting toolbar.

    I did that wrap my code in <code> tage can tell me what the next step to apply the filter on the all query

  • User Avatar
    0
    ismcagdas created
    Support Team

    Could you show your GetCurrentCustomerIdOrNull method ?

  • User Avatar
    0
    jehadk created

    Can you please support me with sample of code working on the ASPZero version 3.5

  • User Avatar
    0
    ismcagdas created
    Support Team

    @jehadk,

    Unfortunately we don't have a sample for this. What is your latest problem, we can try to help.