zzzprojects / EntityFramework.DynamicFilters

Global filtering for Entity Framework.
https://entityframework-dynamicfilters.net/
MIT License
501 stars 86 forks source link

Cannot use a DBContext property in a condition for a dynamic filter. Value dosen't update after initial execution. #166

Closed plemm98 closed 4 years ago

plemm98 commented 4 years ago

Description

Let's say I need to filter a table that contains a column call "clientid". I got a DBContext file that look like this :

public class SampleModel: DbContext
{
       public Guid ClientId { get; set; }

       public SampleModel(Guid clientId) : base("SampleModel")
       {
            ClientId = clientId;
       }

       .......................

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
            .........
           modelBuilder.Filter("ClientFilter", (Client client, Guid clientId) => (client.Id == clientId), () => this.ClientId);
       //Let's put true all the time
        modelBuilder.EnableFilter("ClientFilter", () => true));
    }
}

As you can see, i'm passing the clientId that need to be use in the filter condition directly in the constructor of my dbcontext. Then, I set the value to the property of my dbcontext. Finally, i'm using this property in my dynamic filter.

When I execute this line of code in my program.cs, the value of "clientid" get set properly the first time the model is actually call, but the second time, it's still using the first value. It's like the dbcontext is cache, and i cannot change it's value.

           //The filter will use this clientid "80fa1dc5-d6de-e811-8607-0003ffb4c0d6"
            using (var model = new SampleModel(new Guid("80fa1dc5-d6de-e811-8607-0003ffb4c0d6"), ppp.GroupId))
            {
                var test = model.Clients.ToList();
            }
            //Even if i change the client id, the filter is still using the previous client id..
            using (var model = new SampleModel(new Guid("81FA1DC5-D6DE-E811-8607-0003FFB4C0D6"), ppp.GroupId))
            {
                var test = model.Clients.ToList();
            }

So, my understanding, is that I cannot store the client id that i want to use directly in the constructor of my dbcontext. It's seem the filter process will use the one in the cache and keep using the first client id I input the first time the model was invoke for eternity. Or, until i close my application.

Where is the best place to store the clientid, to make sure the dynamic filter will pick up the appropriate value when it execute? This value will change quite often during the execution of a single method.

Best regards,

EF 6.4.0 Latest version of dynamic filters.

JonathanMagnan commented 4 years ago

Hello @plemm98 ,

I believe you are looking for the SetFilterScopedParameterValue

You will be able to set it in your context constructor.

See some examples:

Let me know if that helped you to find your solution.

Best Regards,

Jon


Performance Libraries context.BulkInsert(list, options => options.BatchSize = 1000); Entity Framework ExtensionsEntity Framework ClassicBulk OperationsDapper Plus

Runtime Evaluation Eval.Execute("x + y", new {x = 1, y = 2}); // return 3 C# Eval FunctionSQL Eval Function