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
DataContextclass is a child ofDbContext. As such anywhere you needDbContext, simply pass your instance ofIDataContextasDbContext. This may require a soft cast if your variable is of typeIDataContextand notDataContext, which it normally should and will be. - In Highway.OnRamp.MVC.Data we subclass the default
DataContextclass of Highway.Data.EntityFramework into a class calledHighwayDataContextto change the constructor a bit. As such, the type of of yourDbContextisHighwayDataContext, but as above anyIDataContextcan be cast toDbContextwith success.