locked
How to add multiple data by EF6 RRS feed

  • Question

  • User828542847 posted

    is there any provision in EF6 by which i can commit multiple data to db?

    if not available then how could i add single data in loop.

    suppose i have multiple customer data in list then how to iterate and save each data in db?

    it will be helpful if any sample code would be posted.

    Monday, July 3, 2017 5:31 PM

Answers

  • User753101303 posted

    Hi,

    EF works already this way. You can add multiple objects to your DbContext (possilby using http://www.entityframeworktutorial.net/EntityFramework6/addrange-removerange.aspx) and the whole "graph" (ie "registered" objects but also related entities) is saved to the db when SaveChanges is called.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 3, 2017 6:47 PM
  • User753101303 posted

    It just generate and execute the needed SQL statements (so here 3 insert statements) wrapped into a single transaction so that all changes are done or no change at all is done.

    Try https://msdn.microsoft.com/en-us/library/ee712907(v=vs.113).aspx for all kind of information about each EF feature. For example here https://msdn.microsoft.com/en-us/library/dn456843(v=vs.113).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 3, 2017 7:49 PM
  • User1771544211 posted

    Hi tapan2017,

    tapan2017

    if we add multiple data to db this way with EF does the EF will hit db repeatedly?

    No, EF will hit the database when you call the ctx.SaveChanges() method. So the above code will only hit the database once.

    To improve the speed of import multiple records into database, you can do the following settings:

    1. Disable auto change-tracking.

    ctx.Configuration.AutoDetectChangesEnabled = false;

    2. Use AddRange if you are using EF 6 instead of above code. EF 6’s .AddRange() command is much faster because it is optimized for bulk insert.

                using (var ctx = new TestEFContext())
                {
    
                    ctx.Employees.AddRange(oEmp);
    
                    ctx.SaveChanges();
                }

    Best Regards,

    Jean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 11, 2017 6:16 AM

All replies

  • User753101303 posted

    Hi,

    EF works already this way. You can add multiple objects to your DbContext (possilby using http://www.entityframeworktutorial.net/EntityFramework6/addrange-removerange.aspx) and the whole "graph" (ie "registered" objects but also related entities) is saved to the db when SaveChanges is called.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 3, 2017 6:47 PM
  • User828542847 posted

    i have done the job this way but curious to know how EF committing multiple records in db ?

    does ef insert one records at a time or is it insert 3 records at a time ?

    any transaction is maintain in this case. say suppose when ef try to insert 3rd records then some error occur then what will happen? full roll back will happen or something different will occur?

                List<Employee> oEmp = new List<Employee>
                {
                    new Employee{Name="New employee2", Salary=5000},
                    new Employee{Name="New employee3", Salary=6000},
                    new Employee{Name="New employee4", Salary=7000}
                };
    
                using (var ctx = new TestEFContext())
                {
                    foreach (Employee emp in oEmp)
                    {
                        ctx.Employees.Add(emp);
                    }
                    ctx.SaveChanges();
                }
    

    share the knowledge how EF works

    Monday, July 3, 2017 7:01 PM
  • User753101303 posted

    It just generate and execute the needed SQL statements (so here 3 insert statements) wrapped into a single transaction so that all changes are done or no change at all is done.

    Try https://msdn.microsoft.com/en-us/library/ee712907(v=vs.113).aspx for all kind of information about each EF feature. For example here https://msdn.microsoft.com/en-us/library/dn456843(v=vs.113).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 3, 2017 7:49 PM
  • User828542847 posted

    the above operation can be called as bulk operation as per my posted code or not?

    Tuesday, July 4, 2017 1:19 PM
  • User828542847 posted

    see the code

    List<Employee> oEmp = new List<Employee>
                {
                    new Employee{Name="New employee2", Salary=5000},
                    new Employee{Name="New employee3", Salary=6000},
                    new Employee{Name="New employee4", Salary=7000}
                };
    
                using (var ctx = new TestEFContext())
                {
                    foreach (Employee emp in oEmp)
                    {
                        ctx.Employees.Add(emp);
                    }
                    ctx.SaveChanges();
                }

    if we add multiple data to db this way with EF does the EF will hit db repeatedly? tell me the fact.

    Saturday, July 8, 2017 8:16 PM
  • User1771544211 posted

    Hi tapan2017,

    tapan2017

    if we add multiple data to db this way with EF does the EF will hit db repeatedly?

    No, EF will hit the database when you call the ctx.SaveChanges() method. So the above code will only hit the database once.

    To improve the speed of import multiple records into database, you can do the following settings:

    1. Disable auto change-tracking.

    ctx.Configuration.AutoDetectChangesEnabled = false;

    2. Use AddRange if you are using EF 6 instead of above code. EF 6’s .AddRange() command is much faster because it is optimized for bulk insert.

                using (var ctx = new TestEFContext())
                {
    
                    ctx.Employees.AddRange(oEmp);
    
                    ctx.SaveChanges();
                }

    Best Regards,

    Jean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 11, 2017 6:16 AM
  • User828542847 posted

    thanks for your suggestion but you did not use AddRange.

    Tuesday, July 11, 2017 10:28 AM
  • User1771544211 posted

    Hi tapan2017,

    Sorry about my mistake, I modified my previous code.

    Best Regards,

    Jean

    Wednesday, July 12, 2017 1:13 AM