Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

c# - .Net core/EF : cannot access a disposed object. A common cause of this error is disposing a context

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe your OpInfos is getting destroyed. Especially if you have a cascade delete behavior. Why you don't change your code to this:

....your code

if (model != null)
{
var operationId=model.IdOperation:
 context.MethAppointementsPreventifs.Remove(model);
var result = await context.SaveChangesAsync();
if (result==0) return 0; //error

var OpInfos = await context.MethOperations.AsNoTracking().FirstOrDefaultAsync(item => item.Idoperation ==operatonId);
if(OpInfos ==null) return 0; //error

 if (OpInfos.Unité == 1)
....continue your code


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...