How do I get my DbContext?
From time to time it is necessary to either pass a DbContext
to some framework code that expects it, or to know the type of your DbContext
, for instance when setting a database initializer. The answer depends slightly on which part of Highway your using, as follows:
- In Highway.Data.EntityFramework, the
DataContext
class is a child ofDbContext
. As such anywhere you needDbContext
, simply pass your instance ofIDataContext
asDbContext
. This may require a soft cast if your variable is of typeIDataContext
and notDataContext
, which it normally should and will be. - In Highway.OnRamp.MVC.Data we subclass the default
DataContext
class of Highway.Data.EntityFramework into a class calledHighwayDataContext
to change the constructor a bit. As such, the type of of yourDbContext
isHighwayDataContext
, but as above anyIDataContext
can be cast toDbContext
with success.