简体   繁体   中英

Bulk Insert Options

I have a list of Users that I want to import into my app using an excel file.

This process involves several tables namely: Users, Roles, PasswordPolicy, Quotations, DocumentTypes

Currently the flow is as follows:

When I upload my excel, it reads and populates a list of User Objects. This list is then returned and then passed into another function where I create the Roles, PasswordPolicy, Quotations and DocumentTypes.

This is how I create 1 User typically

var applicationUser = new User
 {
     FirstName = StripTags.Strip(Input.FirstName),
     LastName = StripTags.Strip(Input.LastName),
     UserName = StripTags.Strip(Input.UserName),
     Email = StripTags.Strip(Input.Email),
     PhoneNumber = StripTags.Strip(Input.PhoneNumber),
     DateOfBirth = Input.DateOfBirth,
     NormalizedUserName = Input.UserName.ToUpper(),
     NormalizedEmail = Input.Email.ToUpper()
 };

 var result = _userManager.CreateAsync(applicationUser, Input.Password);
 if (result.Result.Succeeded)
 {
     return applicationUser;
 }
 else
 {
     return null;
 }

However, the problem is that with only 1000 rows in my input Excel, it can take more than 20 minutes to create this.

I am trying to search for a Bulk Insert option for my other tables to speed it up but so far I only found one solution that is a paid one.

I use MySQL. Is there any bulk insert efcore library for .net core 2.2?

I have also tried to use the SqlBulkCopy method but I keep getting an exception stating that the

Keyword 'port' is not supported

This is the sqlbulkcopy method

var cp = new[] {
     nameof(UserDocumentCategory.Id),
     nameof(UserDocumentCategory.DocumentCategoryId),
     nameof(UserDocumentCategory.UserId),
     nameof(UserDocumentCategory.TenantId),
 };
 using(var sqlCopy = new SqlBulkCopy("server=localhost;port=3306;database=AppOne;user=root;password="")
 {
     sqlCopy.DestinationTableName = "[UserDocumentCategory]";
     sqlCopy.BatchSize = 1000;
     using(var reader = ObjectReader.Create(Input,cp))
     {
         sqlCopy.WriteToServer(reader);
     }
 }

What other options do I have?

  1. You should create a list of ApplicationUsers
  2. Remember to set the following fields as well other than you're setting above

PasswordHash = hasher.HashPassword(new ApplicationUser(), "RandomPassword");

Id = Guid.NewGuid().ToString();

SecurityStamp = Guid.NewGuid().ToString();

Here hasher is the following, keep the hasher out of insert loop and use hasher in loop to set each User object

var hasher = new PasswordHasher<ApplicationUser>();
  1. Bulk insert using Context.AddRange(listOfUsers)
  2. List item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM