Reputation: 11
Is it possible in Entitiy framework to filter datasets of an entity when initializing the DbContext?
I've got following issue: I want to read production data from our ERP system's database. All of our departments save their data in one table, which has a field for the department the data belongs to. Now I'm developing a software for just one specific department. Of course I could read all data in a DbSet and then query just the datasets of this department in my application code, like this:
Dim test As New DB.ModelProd()
Dim cl As New List(Of DB.Charge)
cl = test.Charges.ToList().Where(Function(ch) ch.Company = 3 And ch.ChargeId IsNot Nothing).ToList()
'do something with cl
But since this is an application for just one department I don't want to read and touch data from others which I never will use. Also I would like to hide this detail from the application because it's error prone (forget to filter the data before using it, etc.).
So, what's the best way to solve this?
Best Regards, Ronald
Upvotes: 0
Views: 158
Reputation: 528
Implement IDbCommandTreeInterceptor and change the DbCommandTree generated by EF. You can find more detailed information on the actual steps in the following blog post.
Upvotes: 0
Reputation: 152491
There's not a "best" way - but here are some options:
Using a repository is probably the most idiomatic way in a multi-tier system. Using stored procedures would be more secure since you can grant access only to the procedures and prevent anyone from using the context directly or querying the database without going through them, but it makes the programming harder (you won't be able to use Linq freely to query the database - only through the stored procedures)
Upvotes: 1