Access DbContext from IQueryable

I am trying to implement a caching pattern that Redis might need to use. The problem with this pattern is that I need to disable Configuration.ProxyCreationEnabled and then turn it back on to avoid any problems in the web farm.

I would like to access DbContext from IQueryable, so I can do it once, not everywhere. The easiest way to do this is to pass the DbContext to my caching extension, however I came across this message:

Access DataContext for IQueryable

Is there a way to access the DbContext similar to the link above using the EF 4.1 Code Fist (DbSet, etc.)?

I tried to find this myself, but tried to find the base class from the referenced DbSet in IQueryable using reflection.

+6
source share
1 answer

The solution mentioned in the Access DataContext for IQueryable is a hack and should not be used. It relies on the variable name of a private member in a class that implements IQueryable. This means that the implementation class may change in a future version of the EF / .NET Framework and break your code. Because DbContext is not accessible through the IQueryable interface, you must pass it to the caching extension to avoid assumptions about the implementation of IQueryable. It will also allow you to more clearly establish the dependency on DbContext in your caching interface, rather than bury it in the implementation.

+5
source

Source: https://habr.com/ru/post/896651/


All Articles