1

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 ?

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

5 Comments

when i changed it like so, "context.MethAppointementsPreventifs.Remove(model)" works fine and the "operationId"is correct , but in the "await context.SaveChangesAsync()" it throws the exception
Yes, dbcontext never throws exception at Remove(model), only at SaveChanges(). Did you try my code. You can get some more info from it.
Yes, but i had the same exception so i tried to get "OpInfos" between "Remove(model)" and "SaveChanges()" to check my context but i got the same exception on "OpInfos" too
Can you show MethOperations and MethAppointementsPreventifs classes please. It is hard to undestand what are trying to accomplish.
I solved it, in fact this methode was deep in the call stack, i had one of the methodes async void, so i had to make all of the methodes in the call stack async Task not just this one

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.