We are looking for a way of automatically filtering all CRUD operations by a tenant ID in Entity Framework.
The ideas we thought of were:
- Using table valued user defined functions
- Using stored procedures (but we don’t really want to, as we’re using an ORM to avoid doing so)
- Some how modifying the templates used to generate the SQL to add a where clause on each statement.
- Some how modifying the templates used to generate the LINQ in the controllers (we may use MVC).
Any tips?
-thanks
Alex.
Table valued function are only available in .NET 4.5 Beta (and not available in code first). Using them will still not help you because you will have to use the function in every LINQ query so it is the same as using where clause.
It can be useful for some special complex queries but generally it is not what you want.
Too complex and on completely different level of abstraction.
Close to ideal solution. You simply need to wrap access to your entity set into some code which will look like:
Sometimes this is core for something called Generic repository but it is really just a wrapper around EF set. You will always use
GetQueryto query your data store instead of usingDbSetdirectly.