0

I'm using EntityFramework.Filters package in order to set some defaults and dynamics filters to my entities. It's working well, but Q: when I dynamically set a new filter parameter it's being ignored keeping the first value set.

In debug mode CultureHelper.GetCurrentCulture().Key is properly returning the new culture set, the filter parameter is being set, but nothing happens.

Code

public partial class DataModel : DbContext
{
    public DataModel() : base("name=DataModel")
    {
            var currentLanguageId = CultureHelper.GetCurrentCulture().Key;
            this.EnableFilter("HideDeleted");
            this.EnableFilter("CurrentLanguage").SetParameter("currentLanguageId", currentLanguageId);
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        DbInterception.Add(new FilterInterceptor());
        modelBuilder
            .Conventions
            .Add(
                FilterConvention.Create<IDeleted, int>("HideDeleted", (e, Deleted) => e.Deleted == false),
                FilterConvention.Create<ILanguage, long>("CurrentLanguage", (e, currentLanguageId) => e.LanguageId == currentLanguageId)
            );
    }
}
10
  • Did you tried to make currentLanguageId static variable and use it instead of passing? Commented Aug 17, 2017 at 18:47
  • Yes I did @ASpirin, using as static works perfectly. Filter parameter is considering the first value set only. Commented Aug 17, 2017 at 18:48
  • What is the code under CultureHelper.GetCurrentCulture()? Commented Aug 17, 2017 at 19:13
  • Exactly this public static KeyValuePair<long, string> GetCurrentCulture()         {             var currentCultureCode = Thread.CurrentThread.CurrentCulture.Name;             var currentCulture = _cultures.Where(c => c.Value == currentCultureCode).FirstOrDefault();             return currentCulture;         } Commented Aug 17, 2017 at 19:32
  • Don't you think that accessing _cultures could force ModelCreation? Commented Aug 17, 2017 at 19:43

1 Answer 1

1

I could reproduce this issue only by accessing any Context property before enabling the filter in constructor, that causes OnModelCreation to be raised, after OnModelCreation was raised there is no possibility to setup the filter. Debug you OnModelCreation, and define what causes ModelCreation before you filter is enabled. Just set a breakpoints to Constructor and OnModelCreation and you'll see when OnModelCreationis actually started.

Filtering is applyed to the set of objects. Interceptor is built based on context Set property See dude Expression variable. I suppose you are doing retrieves of related items. Something like Device.Descriptions in that case it returns all related records without interception.

If you are using custom hardcoded values it would be better to use Thread.CurrentThread.CurrentCulture.LCID in your filter, that will be standartized and in case of language change in the app filter would be changed at the same moment.

Sign up to request clarification or add additional context in comments.

7 Comments

I'll try all approaches and suggestions asap and feedback your post. Thank you.
sorry about late response. I made a lots of tests here and I deduced that no matter how many time we EnableFilter, what is counting is OnModelCreation combined with this. Another thing I have deduced is that no matter if I use Thread.CurrentThread.CurrentCulture.LCID or CultureHelper.GetCurrentCulture().Key or "AnyStaticValue", the first value is the only one filter considered. I guess there would have only one way to "fix" it - rerunning OnModelCreation when we have to change the filter. Is that possible?
Sorry didn't get your idea? I wouldn't have much time next days? could you maybe sent me a shot project that has the issue at aspirin.gm(at)gmail.com. I'll take a look when I'll got some time
I've tried to store modelBuilder and initialize filter after one more time, but it doesn't helps. The only thing is that Filters should be enabled before OnModelCreation is raised. You should define who triggers that call and get rid of that
Exactly @Aspirin! But that's the point - OnModelCreation only runs once. Setting the filters before it's execution, the filters works as expected. The problem is that I may have to change filter value AFTER OnModelCreation execution. So, I found a possible workaround execution System.Web.HttpRuntime.UnloadAppDomain(); all the time I have to change the filters. Still don't know if is a good approach, btw it's working. I really appreciate your help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.