Solved

ADO.NET Entity Framework Add records efficiently.

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

13 Experts available now in Live!

Get 1:1 Help Now