Home » c# » c# – Entity Framework: Improving Performance when inserting a record in a table with many records-Exceptionshub

c# – Entity Framework: Improving Performance when inserting a record in a table with many records-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I want to insert a single record into a table with 60 columns (some of them are not required) and 2 million records. It takes about 20 seconds.

var package = new Package()
      {
          DestinationCityId = city.Id,
          OriginCityId = RequestDetails.User.Retailer.CityId,
          State = state,
          CreatedOn = RequestDetails.DateTime,
          CreatedByUserId = RequestDetails.User.Id,
          OrderedOn = RequestDetails.DateTime,
          CustomerName = "a",
          CustomerAddress = "a",
          CustomerEmail = "a",
          CustomerPostalCode = "a",
          CustomerLandPhone = "a",
          CustomerCellPhone = "aa",
          CustomerMessage = "a",
          CustomerSubstitute = "a",
          RetailerPackageDiscount = 0,
          Barcode = "2",
          InternalBarcode = "11111",
          PackagingWeight = 10,
          DeliveryType = PackageDeliveryType.ByPostExpress,
          ProvidedByWarehouse = false,
          NeedReview = false,
          NeedReviewType = NeedReviewType.Unknown,
          RetailerId = retailer.Id,
          Retailer = retailer
      };

DbContext.ZPackages.Add(package);
DbContext.SaveChanges();

Any Idea to improve performance?

How to&Answers:

Partitions, Indexes, Async CRUD operations where it possible. This is not EF problem

Answer:

You could also have a look at the EfCore Bulk Extensions project if you’re using EF Core
https://github.com/borisdj/EFCore.BulkExtensions