How to perform bulk insert and batch update or delete operations on Entity Framework

In the previous article, How to easily add Auditing to an Entity Framework Code-First project, we created an ASP.NET MVC web application project and installed DataKitFramework. We also configured the application for entity framework code-first and created a Product model. Also, we did some quick scaffolding and editing for the purpose of showing how easy it can be to integrate Auditing using DataKitFramework. I would recommend going through the previous article to get up to speed with what we are about to do. But if you are good to go, then tag along.

Bulk Insert:

It is no doubt that Entity Framework is a fantastic ORM with a lot of features to simplify database integration to .NET projects. But most developers using this framework hit the wall when their application start demanding manipulation of large records as they are already neck-deep to wiggle their way out. No need to worry, DataKitFramework is here for you. Enough story telling, let's dive into codes. I'll be using the project template from the previous article,  How to easily add Auditing to an Entity Framework Code-First project . Endeavour to check it out if you're not up to speed.

From the Package Manager Console (PMC), enter the following command:

update-database -TargetMigration: 0

This command resets the application database, deleting all records, tables and migration history. It is critical we perform this action because we are about to seed the database with some products and the Seed method of the Configuration class would not be invoked if the database already exists.

Now modify the Configuration.cs file under Migrations with the following codes as illustrated below by first adding the namespace:

using DataKitFramework.Extensions;
using System.Collections.Generic;

and updating the Seed method with the following codes:

I'm using a few records for the purpose of simplicity, but you can add as many records as possible or if you have an external source of data (maybe an excel file), you can extract and bulk insert them. There are no limits to the number of records you can bulk insert.

Now if we run our project and navigate to products on the browser url, entity framework initializes our database and invokes the Seed method of the Configuration class which performs the bulk operation and our product list now populates with a list of products.

Batch Update:

If we try to edit any product on the list, we see an error page. I think we should all be accustomed to this but don't worry, I'll give a brief explanation to why it happened.

Our Product model inherits from DataKitFramework.Audit class and this class has some properties that are not directly exposed to you like CreatedOn, ModifiedOn, RowVersion etc. These properties are used by DataKitFramework to manage auditing, data concurrency, futuristic features like synchronization etc. On post-back of the model, these properties come with null or out of range values and entity framework invalidates the model.

To resolve this error, let's use DataKitFramework batch update feature, so we can focus on the properties of interest.

Add the namespace in the ProductsController.cs file:

using DataKitFramework.Extensions;

then in the Edit method under HttpPost attribute, modify the codes as illustrated below:

The Bind attribute for the Product parameter protects your application from overposting (check this out for more details). While this is excellent, DataKitFramework also protects your application from overposting by using strongly-typed properties update so you may as well choose to omit the Bind attribute from your method. It's your decision.

Now run the project and try to edit a record again. Everything should be OK.

Batch Delete:

Entity Framework performs a delete operation by first fetching the record from database and then deleting the record from database. Two round trips to the database are required to delete a record which can be a performance issue when working with large records. There are other ways to play with the entity state to reduce the round trip to one thereby improving performance, but when large records are involved the aim would always be defeated. DataKitFramework performs the delete operation in one trip irrespective of the number of records.

Modify the DeleteConfirmed method in ProductsController.cs file:

Finally if we run the project and delete a record, everything should work correctly.

In my next article I'm going to dive into simple settings as applications need to preserve scope and application wide settings to meet the needs of our varying clients using our application. Enough said! Coders get cracking and have a lovely day.

Share this post:

Facebook Google +

Get started in a Jiffy

Free articles on our software products

Get the most comprehensive tutorial on our products. Learn how to do the heavy-lifting with our written version articles. Video version is coming soon.

Get Started
Recent articles