The OnConfiguring method provides a fall-back mechanism for configuring a DbContext in Entity Framework Core. In this section, Phil explains how this works, shows the scaffolded OnConfiguring method, and warns about possible side effects of using it in production code.
- [Instructor] You've already seen the OnConfiguring method,…as that is how our context is configured…when we scaffolded the classes…from the AdventureWorks database.…This is essentially a fallback configuration mechanism…for a DbContext instance that runs after construction…and if the DbContext was configured through the constructor,…IsConfigured is set to true.…Be careful when using OnConfiguring in production code,…'cause this can introduce unexpected results.…
Typically, your OnConfiguring code will point…to your development database or have some other settings…in there that you are testing with.…It is much better to use a design time DbContext factory…to set the configuration and we will cover that…in the next section.…Once again, here is the OnConfiguring method.…We talked about how it works, now I want to briefly mention…when to use it.…
It's great for testing and we've been using it…throughout this course.…Not so great for production, because if something…should happen with your dependency injection,…then you have a hard coded connection string.…
Released
6/6/2018- Persisting data
- Tracking EntityState
- Configuring the DbContext
- Transactions across DbContexts
- Understanding the batching process
- Attaching and deleting detached entities
- Design time configuration
- Concurrency checking
- Using custom execution strategies
Share this video
Embed this video
Video: The OnConfiguring method