In Entity Framework Core, the startup time of a large data context represents a significant performance constraint. As a result, instead of using a single vast data context, you should break the data context into numerous smaller data contexts. Entity Framework allows you to build these relationships using navigation properties in your classes representing the tables. Moreover, by convention, Entity Framework will provide the foreign key relationships in the database for you.
DbContext is a class provided by Entity Framework to establish connection to database, query the db and close connection. The OnModelCreating() method allows us to configure the model using ModelBuilder Fluent API. Again, in our upcoming article, we will discuss more about this method. The OnConfiguring() method allows us to select and configure the data source to be used with a context using DbContextOptionsBuilder.
DbContextOptionsBuilder
It's not the end of the world but it certainly complicates DI container configuration. Having stateless services provides tremendous flexibility and makes the configuration of their lifetime a non-issue (any lifetime would do and singleton is often your best bet). As soon as you introduce stateful services, careful consideration has to be given to your service lifetimes.
If your service method forces the creation of a new DbContextScope and then modifies persistent entities in that new scope, it must make sure that the parent ambient scope (if any) can "see" those modification when it returns. By injecting it into whatever layer implement your data access, you're making that layer, and by extension all the layers above which would be pretty much the entire application, stateful. Particularly if your application uses multiple DbContext, resulting in service methods potentially requiring two or more mandatory DbContext parameters.
How DbContextScope works
So you're effectively back to the explicit DbContext approach discussed earlier. I can think of a few ways in which this could be solved but all of them feel more like hacks than clean and elegant solutions. Prior to EF6, using TransactionScope was the only practical way to control the database transaction scope and isolation level. An obvious side-effect of manually controlling the database transaction scope is that you are now forcing the database connection and transaction to remain open for the duration of the transaction scope. This EF behaviour can result in subtle bugs as it is possible to be in a situation where queries may unexpectedly return stale or incorrect data. On the other side, it dramatically simplifies the issue of database transaction lifetime management.
The DbContext exposes the Database object which allows us to create and initialize the database using various initialization strategies. We can also create our own custom strategy to initialize the database. The database initializer also allows what is entity framework us to seed the database with the initial data. The OnModelCreating method allows us to configure the model using DbModelBuilder Fluent API in EF 6. In the next article, I am going to discuss the DbSet in Entity Framework with Examples.
This class is inherited from the system.data.entity.dbcontext namespace. The DbContext is part of the EntityFramework.dll assembly and is installed separately using the NuGet package Manager. The following is an example of SchoolDBEntities class (context class that derives from DbContext) generated with EDM for the SchoolDB database in the previous section.
- This is because the ambient DbContextScope will flow through all the threads your parallel tasks are using.
- Entity Framework's default behaviour is the equivalent of setting Session.FlushMode to Never in NHibernate.
- There is no ambiguity at to what a unit of work means at the database level.
- But using an instance-per-form lifetime in a desktop application, which you'll often find suggested, is a lot more questionable and requires careful thought before being adopted.
- That means it acts as a bridge between your domain classes or entity classes and the underlying relational database.
As we know Entity Framework is build upon ADO.NET so EF also manage a Connection Pool. DbContext open the connection whenever needed at the time of communication with Database. Many times multiple operation done by single connection and vice versa. As it is managing the Connection pool we need not to worry about the connections.