I am building a multi-tenant application using EF 4, I have a shared database an many entities have a CustomerID field. I was wondering if there was a way I could enforce a CustomerID query at my DBSet level from my DBContext.
I was hoping I could specify a CustomerID when I create my DB context, and wrap the DBSets to always include a (x => x.CustomerID == custID) so I don’t need to worry about adding this logic all over my application, but I haven’t been able to find anything that suggests its possible
Many thanks
I strongly recommend doing this in a separate service layer on top of your DbContext since it will be much easier to enforce at that level.
If you insist on doing it at the DbContext level, here is a sample that does what you want.
The big problem with this is that you cannot ensure that the query returned from MyEntities won’t be further modified to include entities for other customers.