It uses EDM in building SQL queries from LINQ queries, building INSERT, UPDATE, and DELETE commands, and transform database result into entity objects. In this article, we will see how to use Entity Framework in a C# application. The CommitAsync() and RollbackAsync() methods will help us implement the unit of work pattern. When we start an operation, we should request an https://deveducation.com/ ITransaction object from our database provider and when we finish we should call CommitAsync() on our transaction. A transaction should also be IDisposable, because if we forget to call the CommitAsync() or RollbackAsync() methods it would leave the transaction running. If we implement the Dispose() method correctly, in such a case the transaction will be rolled back.
We used to open a connection to the database, create a DataSet to fetch or submit the data to the database, convert data from the DataSet to .NET objects or vice-versa to apply business rules. Microsoft has provided a framework called “Entity Framework” to automate all these database related activities for your application. EF API does not create any column for the collection navigation property in the related table of an entity,
but it creates a column in the table of an entity of generic collection. For example, the following Grade entity contains a generic collection navigation property ICollection.
In this scenario, a different context is used to retrieve and save data. One instance of context class is used to retrieve the data, and another is used to save the data. EF API is responsible for maintaining the states of entities throughout their lifecycle. Each entity possesses a state according to the operation performed by the context class. The entity state can be represented by an enum System.Data.Entity.EntityState in EF 6.
We request the IOrderRepository and the IUnitOfWork interfaces from the DI container. Then we call the BeginTransactionAsync() method, followed by the operation, namely creating a new order. By using the Scoped lifetime, inside the same scope (or request for web applications) the UnitOfWork instance will remain the same. In most cases this is the desirable behavior, however, we can also take advantage of the Transient lifetime. The only lifetime we should avoid is the Singleton, since if we forget to complete the unit of work, the transaction will remain open for the entire lifetime of the application.
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. BeginTransactionAsync() should be called at the start of every operation, but only once. So first, we check whether there is an ongoing transaction already, and if yes, we throw an exception. The most important thing to note here is, that we do not start and commit a transaction inside the repository. Therefore, the addition of an entity won’t persist immediately, rather it will be held in memory until we call CommitAsync() on the underlying ITransaction.
Entity Framework is Microsoft’s recommended data access technology for new applications. EF API will create a column in the database table for each scalar property, as shown below. In this article, we saw the features of EF and how to create the Entity Framework. Also, it is a time-consuming, error-prone process, so Microsoft offers a Framework called Entity Framework to automate the entire DB-related work for our application.
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. The GetPluralName() method is also simplified, it’s just an example of how to use an entity’s name as a table name dynamically. 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.
Forty-one percent of C# developers use EF Core, which is beloved as an abstraction layer to simplify working with data during development. We started with an explanation what is entity framework of the pattern and summarized the pros and cons of using it. Then we implemented the pattern without using any specific ORM and tested the behavior of deferred saving.