BULK CRUD Operations using Entity Framework Core | Bulk Operations in ASP.NET Core | EF Code First

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends welcome back in today's session we are going to discuss about bulk operations in Entity framework core whenever we have large number of Records we are going to use this bulk inset or update or delete so today we are going to discuss on that I am going to create one new project here so I'm going to select asp.net core web application with MVC this is my project name I'm going to use the latest framework to perform these operations we are going to use the Entity Framework code first approach I am going to add the references here right click on the project go to the manage Nuit packages here we are going to install required packages for Entity Framework here I am selecting Entity framework core which is having latest version and for the database we are going to use SQL Server I am installing this package also I'm going to use the code first approach that's why I'm installing tools package to use the bulk operations we need one more package called EF code.bulk extensions by using this package we are going to perform the bulk operations install this so we have installed all the required packages now I am going to create one model here for that I'll create a folder called data access layer inside this I am going to create another folder for entities which I am going to store all my DB entities here inside this entities I am going to create a class here I'll make it as employee here I am going to define properties this is going to be ID and this will be primary key here we have to add the required name spaces set data annotation I am going to add another property which is name so these three Fields I am taking for employee here we have to add the connection string to migrate this table I am going to add the connection string inside this app setting.json so which already we used in our previous example so I can copy from this I am going to use it here here I am going to change this database name so I'll take this one as my DB name now I am going to add DB context I'll create a class for that this application DB context I need to inherit from DB context here I am going to generate a Constructor for this with options here I want to initialize my employee table which is DB set I'll make it as employees here we have to add the namespace which is dial dot entities now we have to add the dependency injection inside of program.cs that also I will copy it from my previous example and go to program.cs here I am going to add the dependency injection here we have to pass our DB context name this is my DB context name go to the controller I am going to reuse this home controller I'll remove everything here I don't want all these things I'll keep only index action method here I am going to initialize my database context to access the employee table this I am going to add it inside the Constructor now I can access employee table from controller also I need stopwatch here to check the time how much it takes to perform the inset or update or Delta operations this also I am going to inject into our Constructor also we have to add dependency injection for this stopwatch for that I am going to add here I have to pause stopwatch here I am going to create one method to insert the data I'll make it as a sink which will return string going to make it as insert here I am going to pause list of employees here I have to add the missing namespace here I am using for Loop here I will pass the number of Records I'll make it as ten thousand here I am going to add employees to the list one by one here I am going to pause employee name here I'm hard coding employee names I'll pause the I here so first one it will added as employee underscore 0 then it will be incremented by one and designation also will make it like this so after reading all the employees here I am going to insert the data here I am going to use bulk insert async I will pause the employee list here so here before performing in set operation I am going to start the timer here Dot or else we can pause it here also here only we are going to insert the records so after inserting I am going to stop the timer here finally I will return the time how much it takes for in session here I am going to display how many records it's inserted so this one we can pause it from here I am going to call it from index so here I can pause int total records I'll make it 10 000. and from here I am going to pause from here I am going to call the insert method here I'll pause the total records so instead of hard coding this value I'll pause this total recurs here here I can check how much time it is taken to insert the data so this method I am going to call it from index action method here I am going to make it as task since it is a asynchronous method I'm adding as task so from index action method I am going to call this inset method so which I am passing total number of Records need to insert so in this case I am passing 10 000 as total records it will be added one by one and finally this employee list will be passed to the Balkan set function so this will be executed so once it is executed we are stopping this timer and we will get the total time elapsed to insert this records we'll try to build the applications build failed okay here we have to add scoped build the application once again build succeeded now I am going to migrate this database and this table to our SQL server for that I am going to open Package manager here I will add migration and I'll name it as initial so here we have to change the default project default project currently selected as crud with web API here we have to change our project this is our project I'll run the command again so whenever we have multiple applications we have to set the startup project here now I'll update the database here will refresh the database so this is the database which we created now and we have created employee table with three fields so what database on table has been migrated whenever this insert is successfully completed we will get the message like this so we are going to display this to the user for that here I am going to store inside result this result I am going to store it in temp data I'll add the key name as message this is going to be result and this message I am going to display inside our index view I will go to the views go to the home index I'll remove these things here I am going to read message here I will validate is there any message available or not if it is not null then I am going to display inside bootstrap alert run the application here we got the message like total records and Saturdays 10 000 records and time taken to insert zero six seconds we will check the database table I will remove this will make it as select store from this table execute it so 10 000 record has been inserted now I'll try to update these records for that I am going to create another method here I am going to update now we'll make it as update here I have to pass the ID also I'm going to make it as plus one and instead of insert I am going to make it as update total records updated now instead of calling this inset method I am going to call it update I'll comment this insert one and I am going to use update I'll run this here for updation we will change this update employee like this designation also I'll change it currently we inserted as employee and the ID number we inserted for update I am going to add this text so that easily we can check whether these records are updated or not and now we have commented insert method we are going to call this update run this so total records updated 10 000 it taken 6 seconds to update these records and check the table now execute so all the record has been updated here now we will implement this for deletion also I'm going to copy this here I'll make it as delete and here I no need of this one straight away I am going to get the list of employees from the table and this is also not required employee list equal to so where I can read it from context Dot employees table I can make it as lists so I'm going to pause this employee list to this delete I'll make it as bulk delete here we are going to get the total records from the employeelist dot count now I am going to call this delete method from the index View to comment this so we are going to delete all the records which is available inside the table will run the application so total records deleted it's 10 000 and time taken to delete only Milli seconds it's taken to delete the records we will check the database execute the query so we don't have any data we have deleted all the records from the table now instead of making 10 000 I'm making just one lakh records I am going to insert now I'll comment this delete method I'm going to perform insert alone now so now instead of 10 000 we are going to pass One lakh records here I'll run the application so Total One lakh records inserted in nine seconds now I am going to check the database whether these records are inserted or not so we have one lakh records here and now I am going to update these records so we are trying to update this one lakh records run the application so now one like records updated Within 12 seconds now we'll try to delete these records I am going to comment this update method I'll uncomment this delete run the application so total records deleted is one lakh and time Taken 2 seconds we will check the database execute this we don't have any data we have deleted successfully when we have large number of data that time we are going to use this bulk operation that's it for today if you like this video please like And subscribe to my channel thanks for watching
Info
Channel: CodeWithGopi
Views: 1,813
Rating: undefined out of 5
Keywords: asp net core 6 crud, asp.net core 6 pagination, asp.net core ef core, asp.net ef core, asp.net mvc, bulk crud operations using entity framework core, bulk insert in asp.net core, bulk operations using entity framework core, code with gopi, codewithgopi, crud entity framework core, crud entity framework core c#, crud operations in asp.net, crud using bulk operation in asp.net core, dot net core, entity framework, entity framework core, mvc, sql bulk insert in asp.net core
Id: y_a17DqRqIU
Channel Id: undefined
Length: 23min 30sec (1410 seconds)
Published: Wed Mar 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.