Solved

ADO.NET Entity Framework Add records efficiently.

Posted on 2010-08-26
5
1,013 Views
Last Modified: 2013-12-17
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
Comment
Question by:whorsfall
  • 3
  • 2
5 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 33535454
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
 

Author Comment

by:whorsfall
ID: 33538184
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
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 total points
ID: 33538716
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
 

Author Closing Comment

by:whorsfall
ID: 33538966
Awsome great answers.

Thankyou.

Ward
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33543412
Not a problem Ward, glad I was able to help.  ;=)
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question