Having my own EF DbContext
derived class associated with my own DbConfiguration
derived configuration class:
[DbConfigurationType(typeof(MyDbConfiguration))]
public class MyDbContext : DbContext
{
}
I'm trying to access the instance of the MyDbConfiguration
from within MyDbContext
.
(Assuming that there actually is any instance at all).
E.g.:
public class MyDbConfiguration : DbConfiguration
{
public int SomeProperty { get; set; }
}
and
[DbConfigurationType(typeof(MyDbConfiguration))]
public class MyDbContext : DbContext
{
public int SomeMethod()
{
// This lines fails to compile:
var config = (MyDbConfiguration)Configuration;
config.SomeProperty = 42;
}
}
The compilation error is:
Cannot convert type 'System.Data.Entity.Infrastructure.DbContextConfiguration' to 'MyDbConfiguration'.
Obviously, there is no direct relationship beween these two types.
Most likely I'm completely misunderstanding this Entity Framework configuration stuff and this question is an XY problem. Still I'm kindly trying to ask it:
My question:
Is there any way to access the DbConfiguration
from within a DbContext
?
If not, is it possible the other way around? I.e. access the DbContext
from within a DbConfiguration
?
(This question is related to EFCache issue #14)
Being unable to find a direct answer, a workaround suggested by the EFCache author helped me in my situation:
Just store a reference to the cache in a static variable before you pass it to the
CacheTransactionHandler
c'tor and then you should be able to it access from anywhere. The cache is effectively singleton so I don't think there is any problem with doing this.
Not the best solution I was hoping for and stil runs perfectly.