Solved

ADO.NET Entity Framework Add records efficiently.

Posted on 2010-08-26
5
1,009 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 62

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 62

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 62

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now