Comments (10)
I do something like what you are talking about. But you need to define the filter first. Since the filters are dynamic you can change the filter value on the fly. I have a static singleton class that contains my company level filtering, it is set per request.
modelBuilder.Filter("CompanyId", (ICompanyOwned t) => t.CompanyId, () => CompanyRequestManager.Instance.CompanyId);
In my application the request to the service always contains the CompanyId so that I can set the value per request. If you want to get that value from the database on every request then I would suggest toggling the filter on and off.
using (var context = new TestContext())
{
context.DisableFilter("CompanyId");
CompanyRequestManager.Instance.CompanyId = context.CompanyEntity.GetIdSomehow()
// Re-enable and check again
context.EnableFilter("CompanyId");
//run filtered query.
}
from entityframework.dynamicfilters.
You can create your filters before you actually set the value to filter on. Take a look at how the "BlogEntryFilter" filter is configured in ExampleContext.cs of the Example project. It is creating a filter against a static property "CurrentAccountID". The value for that property just needs to be set before you actually execute a query that would invoke the filter (otherwise, in this case, it would use the default value for a Guid).
And it's not necessary to disable filters before doing that.
from entityframework.dynamicfilters.
jcachat If you did not disable the filter before running the query wouldn't it filter all the results by guid.default?
from entityframework.dynamicfilters.
No, the value to filter on isn't read until the filter is actually executed - and it's read each time it's executed (and set into a sql parameter). That is what makes it dynamic vs. the EntityFramework.Filters project which reads the values when the query is COMPILED. So when you use a delegate to get the value of a filter parameter (as in this example), you can freely change it any time you want and the filter will always use the current value each time it executes.
Disabling the filter actually just sets a bool property value on the query that causes the conditions (which have already been compiled into the query) to not be applied at query execution time. If you use SQL Profiler to capture the sql when the filter is disabled vs. enabled, you'll see that is the only difference is in the value of a sql parameter named like "@DynamicFilterParam_[filtername]_DynamicFilterIsDisable".
from entityframework.dynamicfilters.
Let me rephrase, and please correct me if I am wrong. Looking at the OP I understood that he wanted to query the database for the Company Id before the filter that needed the Id is set up. In that instance the first query against the databases to get the Company Id would be filtered by Guid.Empty when the query is executed the first time.
from entityframework.dynamicfilters.
Oh I see what you mean now. I was assuming that the query to get the Company ID is being done against something that is not filtered (some kind of configuration table that contains the Company ID) while the filters are set on entities that have a CompanyID column/FK.
It looks like your first post is much more detailed than the email git sent me so I didn't see all that (maybe you edited). So in your example, if your call to:
CompanyRequestManager.Instance.CompanyId = context.CompanyEntity.GetIdSomehow()
does a query against an entity that is ICompanyOwned, then yes, you need to disable the filter first. If not, you do not need to disable.
That is a good point to make but otherwise it looks like we answered the original question the same way.
from entityframework.dynamicfilters.
Yeah I totally edited my first response after re-reading the OP. =)
from entityframework.dynamicfilters.
Thanks for the fast response! Your code example and the discussion helped me a lot :-)
from entityframework.dynamicfilters.
I created a singelton class that queries for the company id but I get an exception.
The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method..
I understand the problem, the company id is instantly retrieved:
modelBuilder.Filter("GlobalCompanyFilter", (ICompanyReference r) => r.CompanyId, SingletonPerRequest.Current.CompanyId);
And another problem is that disabling the filter wouldn't stop retrieving the value (if retrive is executed in the get of the property and not the constructor) because as @jcachat said:
Disabling the filter actually just sets a bool property value on the query that causes the conditions (which have already been compiled into the query) to not be applied at query execution time.
@siegeon how did you resolve this problem?
from entityframework.dynamicfilters.
I think I solved it:
modelBuilder.Filter("GlobalCompanyFilter", (ICompanyReference r) => r.CompanyId, () => SingletonPerRequest.Current.CompanyId);
from entityframework.dynamicfilters.
Related Issues (20)
- [Oracle EF 6.2] System.MissingMethodException HOT 1
- Conditional filter not working with EntityFramework6.Npgsql 3.2.1.1 HOT 6
- Support for EF 6.3 and netstandard HOT 7
- Context.Set<T>().Find(keyValues) And Use Context.Filter<Interface>() after, HOT 3
- EntityCommandCompilationException when using .Include() on class with subclass HOT 2
- Entity Framework Dynamic Filters not working in dot net core HOT 3
- Another way to initialize filters besides calling modelBuilder.Filter? HOT 1
- Enum-of-byte is unnecessarily cast to int HOT 6
- IsFilterEnabled returns true for non-existent filter HOT 1
- Cannot use a DBContext property in a condition for a dynamic filter. Value dosen't update after initial execution. HOT 1
- Problem creating Filter HOT 2
- Compatibility issue with bulk operations (Z.EntityFramework.Extensions) HOT 6
- Why filters should come last in the model creation? HOT 11
- Filter with claim HOT 3
- Compile error with package reference HOT 2
- Concurrency error when PreventDisabledFilterConditions is called HOT 4
- Issue with dynamic filter applied on eager loading with many-to-many relationship entities using MySQL
- Issue occur when dynamic filter is applied on eager loading with many-to-many relationship entities for MySQL HOT 8
- Filter decoder HOT 1
- ConcurrentDictionary is breaking my Oracle 11g unit tests HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from entityframework.dynamicfilters.