Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1019
  • Last Modified:

ADO.NET Entity Framework Add records efficiently.

Hi,

I have a simple routine to add records to a database using the entity framework. I wanted to make this
perform as fast as possible and wanted to make sure I am doing thing correctly.

To add multiple records to a table should I be creating a new instance of the record object each time.
PidTable pid_table = new PidTable(); so for 100 records a 100 object creations?

Also can I assume that the context.SaveChanges will buffer up all the changes in memory and
write them in one hit. So it is more efficient to have that at the end then within the loop
context.SaveChanges();

Also in regards to SaveChanges() I assume that the more records that are changed the more memory is used. Is there a limit to this is is it governed by the amount of free memory.

Additionally can I reclaim the memory back from SaveChanges() after the context is disposed or do I need to wait for .NET garbage collection.

Any advice greatly appreciated.

Ward.



private void fnAdd_100_Records()
        {
            
            CarDBEntities context = new CarDBEntities();
            
            decimal counter;

            for (counter = 1; counter <= 100; counter++)
            {
                PidTable pid_table = new PidTable();
                pid_table.ID = counter.ToString();
                pid_table.PID1 = (decimal)counter;
                context.AddToPidTable(pid_table);
            }

            context.SaveChanges();
            context.Dispose();

        }

Open in new window

0
whorsfall
Asked:
whorsfall
  • 3
  • 2
2 Solutions
 
Fernando SotoCommented:
Hi whorsfall;

To the question, "To add multiple records to a table should I be creating a new instance of the record object each time. PidTable pid_table = new PidTable(); so for 100 records a 100 object creations?", Yes a new object is needed for each record to be added to the database.

To the question, "Also can I assume that the context.SaveChanges will buffer up all the changes in memory and write them in one hit. So it is more efficient to have that at the end then within the loop context.SaveChanges();", All of the CRUD records are keep in separate collections in the object context until it has been updated to the database. When the SaveChanges method is called the object context opens a connection to the database and batches the records then closes the connection.

To the question, "Also in regards to SaveChanges() I assume that the more records that are changed the more memory is used. Is there a limit to this is is it governed by the amount of free memory.", I have never read anywhere in the documentation that a limit is put on the size of the collections in the object context so I would also assume that it is only constrained by memory size on the system.

To the question, "Additionally can I reclaim the memory back from SaveChanges() after the context is disposed or do I need to wait for .NET garbage collection.", If you are done with the Entity Framework ObjectContext object then yes you could call on garbage collection to do its job but unless it is critical to do so I would not seeming that will cause a performance hit. I would rather enclose the ObjectContext in a using block to close and dispose of the data.

Fernando
0
 
whorsfallAuthor Commented:
Hi,

Great help with your answers, thank you. So overall is I can assume I am doing this is the fastest way possible. Is my placement of the context.SaveChanges(); correct or should I put it within the loop for best performance.

Also u mentioned putting it in a using block is that not the same as using a .Dispose() method,
why is the using block better?

Thanks,

Ward
0
 
Fernando SotoCommented:
Hi whorsfall;

To the question, "Is my placement of the context.SaveChanges(); correct or should I put it within the loop for best performance.", Where it is now is the best placement for performance. I you place it within the loop you will have one hit to the server per iteration of the loop, not what you want.

The using block will call Dispose before leaving the block for whatever reason even when an exception is thrown and causes the object to go out of scope just after executing the Dispose call. When the compiler translate the using block it translate it to a try/finally statement :

try
{
    // Code in the using block
}
finally
{
    // Calls Dispose and other cleanup
}

Fernando
0
 
whorsfallAuthor Commented:
Awsome great answers.

Thankyou.

Ward
0
 
Fernando SotoCommented:
Not a problem Ward, glad I was able to help.  ;=)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now