Solved

ADO.NET Entity Framework Add records efficiently.

Posted on 2010-08-26
5
1,015 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

729 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