Data Access

Two Ways of Handling Concurrency Violations in Entity Framework Code First
When you update an entity and call SaveChanges() to save the values back in the database, the existing values for that record are overwritten by the new entity values. This is the default behavior in entity framework code first. In a real world situation it is quite possible that database values might have got changed after you select them for modification. In such cases, your update operation might be overwriting changes made by someone else. This is concurrency violation. And it is important to detect if concurrency violation has occurred. You can then take some corrective action or at least inform the user about such a violation.
Posted On : 15 Jun 2015
Wrapping multiple calls to SaveChanges() in a single transaction
When you make any additions, modifications and deletions to an Entity Framework DbSet and call SaveChanges(), EF starts a new transaction and executes all the INSERT, UPDATE and DELETE operations inside that newly created transaction. If the call to SaveChanges() succeeds the underlying transaction is committed, otherwise the transaction is rolled back. In some cases you may want that multiple calls to SaveChanges() be executed in the same transaction. Luckily, Entity Framework 6 provides an easy way to accomplish the same.
Posted On : 29 Jul 2014
What to do when SaveChanges() fails?
Beginners often ask this question - "What course of action should I take when a call to SaveChanges() fails?" The answer may vary based on a given situation and requirement. This article discusses one way to deal with the errors and shows how to display descriptive error messages to the end user and then rollback the changes made to the model.
Posted On : 21 Jul 2014
Using Unit of Work Pattern in ASP.NET MVC and Entity Framework
Recently I wrote an article explaining the Repository Pattern and its use in ASP.NET MVC. This article builds on that article and explains another related design pattern - Unit of Work. The Unit of Work pattern states its intent like this - Maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems.
Posted On : 15 Apr 2014
Using the Repository Pattern with ASP.NET MVC and Entity Framework
Data driven web applications need to have a neat strategy for data access. One of the important aspects of this strategy is the separation between the physical database, queries and other data access logic from the rest of the application. Repository pattern is a popular way to achieve such an isolation. This article discusses the basics of Repository pattern in the context of Entity Framework and ASP.NET MVC. It also illustrates how a repository can be built around your entities.
Posted On : 10 Apr 2014
Performing Asynchronous Operations Using Entity Framework
Asynchronous programming involves executing operations in the background so that the main thread can continue its own operations. This way the main thread can keep the user interface responsive while the background thread is processing the task at hand. .NET framework introduced the async and await keywords that simplify asynchronous programming. Entity Framework 6.0 also supports asynchronous operations for querying and saving of the data. This article discusses the basics of using asynchronous operations of Entity Framework in desktop as well as web applications.
Posted On : 18 Mar 2014
Working with Stored Procedures in Entity Framework - Part 2
In Part 1 of this two part series, you learned to map stored procedures for the INSERT, UPDATE and DELETE operations in the entity framework designer. EF also allows you call arbitrary stored procedures from your code. This part of the article discusses how.
Posted On : 12 Feb 2014
Working with Stored Procedures in Entity Framework - Part 1
By default entity framework generates SELECT, INSERT, UPDATE, DELETE queries to perform the respective operations on the database. At times, however, you may want to deviate from this default behavior and use stored procedures for these operations. Additionally, you may want to call arbitrary stored procedures to perform some task at hand. To that end this two part article explains how stored procedures can be dealt with in Entity Framework.
Posted On : 06 Feb 2014
Using Complex Types in Entity Framework
A database table represents your application data in row-column format. Although in many cases this row-column structure can be mapped directly to entities, at times you may need to reorganize the same. Consider, for example, the Customers table of Northwind database. The Customers table has Address, City, Region, Country and PostalCode columns representing the address of the company. The entity that represents this table might not be as "flat" as the table. For example, you may wish to have a property - Location - that is of class type and has properties such as Street, City, Region and PostalCode. Luckily, entity framework allows you to do so quite easily. The remainder of this article explains how.
Posted On : 15 Jan 2014
Executing Raw SQL Queries using Entity Framework
While working with Entity Framework developers mostly use LINQ to Entities to query database. However, at times you may need to execute raw queries against the database. A common scenario is when you wish to generate an SQL query dynamically. Luckily, EF allows you to execute raw queries easily. This article explains how that can be done.
Posted On : 09 Jan 2014