I am using Net core, and am facing this famous problem i have this sample :
var model = await context.MethAppointementsPreventifs
.FirstOrDefaultAsync(item => item.StartDate >= DateTodelete
&& item.IdOperation == XpertHelper.IdOperation);
if (model != null)
{
var OpInfos = await context.MethOperations.AsNoTracking()
.FirstOrDefaultAsync(item => item.Idoperation == model.IdOperation);
context.MethAppointementsPreventifs.Remove(model);
await context.SaveChangesAsync();
}
when i get my "model" and "OpInfos" no context disposed or excpetion thrown but in the line of :
context.MethAppointementsPreventifs.Remove(model);
it throws that exception, my methode doesnt return an async void :
public static async Task<int> ClearAppPreventif(KBFsteelContext context)
{
//clear from the day of intervention
var DateTodelete = XpertHelper.DateIntervention;
bool IsStill = true;
while (IsStill)
{
var model = await context.MethAppointementsPreventifs
.FirstOrDefaultAsync(item => item.StartDate >= DateTodelete && item.IdOperation == XpertHelper.IdOperation);
if (model != null)
{
var OpInfos = await context.MethOperations.AsNoTracking().FirstOrDefaultAsync(item => item.Idoperation == model.IdOperation);
context.MethAppointementsPreventifs.Remove(model);
await context.SaveChangesAsync();
if (OpInfos.Unité == 1)
{
var newDayDate = DateTodelete.AddDays(OpInfos.Fréquence);
DateTodelete = newDayDate;
}//jours
if (OpInfos.Unité == 2)
{
var newDayDate = DateTodelete.AddMonths(OpInfos.Fréquence);
DateTodelete = newDayDate;
}//Mois
if (OpInfos.Unité == 3)
{
var newDayDate = DateTodelete.AddYears(OpInfos.Fréquence);
DateTodelete = newDayDate;
}//Annees
}
else
{
IsStill = false;
}
}
return 1;
}
and am calling it with await... so what should i do ?
question from:
https://stackoverflow.com/questions/65901218/net-core-ef-cannot-access-a-disposed-object-a-common-cause-of-this-error-is 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…