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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With