i want write a api for bulk insert,how can u help me
Loading
i want write a api for bulk insert,how can u help me
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 23, 2023, 3:15 AM EST
Hello Sandip
you can use N.EntityFramework.Extension
Once you install it, you can simply use BulkInsert() method directly on the DbContext instance. It supports BulkDelete, BulkInsert, BulkMerge and more.
var dbcontext = new MyDbContext(); var orders = new List(); for(int i=0; i<10000; i++) { orders.Add(new Order { OrderDate = DateTime.UtcNow, TotalPrice = 2.99 }); } dbcontext.BulkInsert(orders);Refer : https://stackoverflow.com/a/63101773/3054222
Thanks
Naimish Makwana
Sandeep KumarRank #1,642 · 937 reputationPosted Feb 23, 2023, 3:19 AM EST
this my model class
public class AddUserAccess { public List UserAccessList { get; set; } } public class UserAccessList { public string Type { get; set; } public string Page { get; set; } public string Access { get; set; } public string UserId { get; set; } public string IsGranted { get; set; } }this my method ,what i have write in for each
async Task IPaperManager.InsertUserAccess(AddUserAccess Items) { _ResponseStatus = new ResponseStatus(); var createDate = DateTime.UtcNow.Add(new TimeSpan(+5, +30, +00)); foreach (var UserAccess in Items.UserAccessList) { } return _ResponseStatus; }Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 23, 2023, 3:22 AM EST
You can write like this.
Need to create object of dbcontext.
async Task IPaperManager.InsertUserAccess(AddUserAccess Items) { _ResponseStatus = new ResponseStatus(); var createDate = DateTime.UtcNow.Add(new TimeSpan(+5, +30, +00)); //you can write like this. dbcontext.BulkInsert(Items.UserAccessList); return _ResponseStatus; }Sandeep KumarRank #1,642 · 937 reputationPosted Feb 23, 2023, 3:27 AM EST
want to use for each one by one
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 23, 2023, 3:32 AM EST
You want to insert data in bulk. So why you want to use in loop?
As I see public List UserAccessList {get; set;} you're all the data are in list.
If you write code in for each, then there is no use of bulk insert.
Vishal YelveRank #178 · 13.6K reputationPosted Feb 23, 2023, 7:34 AM EST
Hi Sandeep,
I suggest you if you are want to Bulk Insert use EFCore.BulkExtensions.
please refer below article it will help you alot.
https://www.c-sharpcorner.com/article/bulk-operations-in-entity-framework-core/