Using SaveEntityCollection To Save Thousands Of Records

Posts   
 
    
hankddog
User
Posts: 52
Joined: 08-Apr-2005
# Posted on: 28-Jun-2023 17:00:09   

I am using SaveEntityCollection to save over 3000 record, but it takes 4 minutes to save, is there a more optimal way of saving in bulk?

        public string SaveEntityCollection(EntityCollection<MEntity> obj)
        {
            string retval = null; // null indicates success; if this is not null at the end, it will hold the error message
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                try
                {
                    adapter.StartTransaction(IsolationLevel.ReadCommitted, "SaveMEntity");
                    //now save the new object
                    adapter.SaveEntityCollection(obj,false,false);

                    adapter.Commit();
                }
                catch (ORMQueryExecutionException e)
                {
                    if (adapter.IsTransactionInProgress)
                    {
                        adapter.Rollback();
                    }
                }
                catch (Exception ex)
                {
                    if (adapter.IsTransactionInProgress)
                    {
                        adapter.Rollback();
                    }
                    retval = $"General error: {ex.Message}";
                }
            } // the adapter gets automatically disposed here because of the 'using' statement

            return retval; // will be null if everything went fine, or an error message otherwise
        }
Walaa avatar
Walaa
Support Team
Posts: 14954
Joined: 21-Aug-2005
# Posted on: 28-Jun-2023 20:16:46   

You can use batching

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39625
Joined: 17-Aug-2003
# Posted on: 29-Jun-2023 10:16:26   

Additionally, SaveEntityCollection already starts a transaction so you don't have to babysit that yourself. When an exception occurs, it's automatically rolled back.

4 minutes for 3000 rows is very very slow. Please use the ORM Profiler or other profiler to see where the bottleneck is. If it's roundtripping to your server (e.g. because it's on a slow network connection) batching will fix it.

Frans Bouma | Lead developer LLBLGen Pro
hankddog
User
Posts: 52
Joined: 08-Apr-2005
# Posted on: 31-Jul-2023 15:07:18   

Walaa wrote:

You can use batching

That did the trick thanks!