Seleziona una pagina

An instance of DbContext represents a session with the database which can be used to query and save instances of your entities to a database. DbContext is a combination of the Unit Of Work and Repository patterns. The Database instance manages the connection to the database, transactions and all other database related operations. We are pleased to announce that the MongoDB Provider for Entity Framework Core (EF Core) is now available in Public Preview. Usually, the slowest element of a computer system is the communication channel.

So, please modify the main method of the Program class as shown below. In the above code, the optionsBuilder.UseSqlServer() is an extension method used to configure EF to use SQL Server as advantages of entity framework the database provider by specifying a database connection string. We have specified entities as DbSet properties, but we haven’t specified the database name and database server info yet.

Faking DbContext in Tests with NSubstitute

The SqlConnection will be provided by the dependency injection framework. With Dapper, it’s a bit difficult to implement the GetEntitySet() method, since Dapper is not able to defer the evaluation of queries like Entity Framework Core. So when calling the GetEntitySet() method, Dapper will always go to the database and query the table. In a real-life scenario, this is suboptimal behavior, but this code is just for demonstration purposes.

what is dbcontext in entity framework

The Entity Framework Core DbContext class includes a property, i.e., DbSet, for each entity in your application. Returns the instance of the DbEntityEntry object for the given entity. The DbEntityEntry provides information about the entity and also has the ability to perform actions on the entity. DbContext tracks the changes done to each entity in its lifetime.

Entity Framework – DbContext

Later, as we progress, we will add more domain classes to this example. Above, base.OnConfiguring(optionsBuilder) calls the base implementation, which does nothing. We can remove it and specify a connection string for the database in the OnConfiguring(), as shown below. The DbContext is the backbone of the Entity Framework Code First.

  • Then we implemented the pattern without using any specific ORM and tested the behavior of deferred saving.
  • This is possible because the unit of work pattern does a great job of separating data access from our application layer.
  • To delete an entity using Entity Framework, you use the Remove method on DbSet.
  • Once the SaveChanges method execution is completed successfully, it will move the Entity state from Added to Unchanged state.
  • Above, base.OnConfiguring(optionsBuilder) calls the base implementation, which does nothing.
  • This method is called for each instance of the context that is created.

DbContext is a lightweight version of the ObjectContext class, which is laid almost right on top of ObjectContext (there is even a way to get to the ObjectContext from just the DbContext). It’s also a lot easier to use, IMO, and makes CRUD operations a sinch. I though DbContext only handle the connection to the DB and the number of threads working against the DB.

Benefits of using the provider for EF Core

The NuGet package installation and ServiceCollection creation is necessary because we are using a simple console application, but this is created automatically in the ASP.NET Core templates. Similarly to the BeginTransactionAsync() method, we start by checking the _currentTransaction, just now we check whether it is null. If it is, we throw an exception, since we don’t have a transaction to commit.

In this case, it’s perfectly understandable if we don’t want to overengineer the solution, and simply use the database directly. Throughout the article, we talked many times about dependency injection. It is a very important concept when utilizing the unit of work pattern. It enables us to switch implementations fast and easily, even at runtime. Moreover, it helps us manage the lifetime of the unit of work, so we will never forget to close a transaction, even in case of exceptions. BeginTransactionAsync() will start a transaction, and keep track of it inside the unit of work, so the caller doesn’t have to dispose of it.

what is dbcontext in entity framework

(entities containing related entities) When working with entities, References are everything, and things like serialization and re-composition will break this. Loading data with AsNoTracking() can also cause this issue if you go to re-attach the detached entity graphs. Another issue related to delays occurs when we retrieve an entity from the database, make modifications within the unit of work, and then re-query it within the same transaction. In this scenario, it retains the previous values because we haven’t saved the changes yet. Sometimes this complexity increase is not justified, especially in the case of simpler applications. Let’s assume that our application is a single-user console application in a corporate intranet.

what is dbcontext in entity framework