Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to improve performance of data saving in Entity Framwork using db.savechange() funcation in recursive function of C# or other option available?

I am saving and updating records by using a recursive function but it's using too much time - around 2 minutes for saving records. I am using Entity Framework for database access.

Guideline best practice for Entity Framework is most welcome

There is N level of child so we don't know how many child we have so I have tried call db.savechange() after completion of recursive but no luck for this.

I have this type code skeleton (it's not original, I have just added structure).

public void parentFunction(List<DataListforupdate> dataListforupdates, string defaultvalue, Guid perentguid)
{
    if (dataListforupdates != null && dataListforupdates.Count() >)
    {
        foreach (var model in dataListforupdates)
        {
            // manipulate data for save
            saveDateInDB(model.value, model.value, model.value, model.value);

            if (model.child.count() > 0)
            {
                ChildFunction(model.child.dataListforupdates, defaultvalue, perentguid)
            }
        }
    }
    else
    {
        var getAllDataForupdate = db.tbl.where(x => x.gID = perentguid).toList();

        foreach (var model in getAllDataForupdate)
        {
            // manipulate data for save
            saveDateInDB(defaultvalue, model.value, model.value, model.value);

            if (model.child.count() > 0)
            {
                ChildFunction(model.child.dataListforupdates, defaultvalue, perentguid)
            }
        }
    }
}

public void ChildFunction(List<DataListforupdate> dataListforupdates, string defaultvalue, Guid perentguid)
{
    // update model date if its not null
    if (dataListforupdates != null && dataListforupdates.Count() >)
    {
        foreach (var model in dataListforupdates)
        {
            // manipulate data for save
            saveDateInDB(model.value, model.value, model.value, model.value);

            if (model.child.count() > 0)
            {
                ChildFunction(model.child.dataListforupdates, defaultvalue, perentguid)
            }
        }
    }
    // if model date is null then update all record with default value 
    else
    {
        var getAllDataForupdate = db.tbl.where(x => x.gID = perentguid).toList();

        foreach (var model in getAllDataForupdate)
        {
            // manipulate data for save
            saveDateInDB(defaultvalue, model.value, model.value, model.value);

            if (model.child.count() > 0)
            {
                ChildFunction(model.child.dataListforupdates, defaultvalue, perentguid)
            }
        }
    }
}

public void saveDateInDB(string value1, string value2, string value3, string value4)
{
    var getAddedData = db.DataFromTable.where(x => x.id = value1).FirstOrDefault()

    if (getAddedData != null)
    {
        // update date using db.savechanges()
    }
    else
    {
        // Add date using db.savechanges()
    }
}

is it speed issue because of i am calling db for get list in each recursive?

like image 332
Rajdip Khavad Avatar asked Dec 06 '25 19:12

Rajdip Khavad


1 Answers

I have tried call db.savechange() after completion of recursive but no luck for this.

SavingChanges() at once suppose to improve application performance instead of doing savechanges for every child. It does not make sense. On recursive you should only change the state of the entity to modified. Also you can disable change tracking.

Configuration.AutoDetectChangesEnabled = false;
context.Entry(entity).State = EntityState.Modified;

Plus you should get all datas from database at once. On recursive you should not go to database. It's not a best practice.

like image 92
Kadir Avatar answered Dec 09 '25 08:12

Kadir