0

I checked other questions and I am using the same DbContext for multiple actions so this is not the problem.

I have an MVC controller. Inside this controller I am doing the following:

var invites = await (from i in _db.Invitations
                     where i.User.Id == user.Id && i.Flat.Id == flat.Id
                     select i).ToListAsync();
_db.Invitations.RemoveRange(invites); //Error here
await _db.SaveChangesAsync();

Can someone explain to me why the error occurs?

Thanks a lot!

Edit: Thanks for your answers, i will try this. Currently I am instantiating my DbContext like this for each controller in my application.

private readonly ApplicationDbContext _db = new ApplicationDbContext();

Actually I don't like using a singleton for the DbContext. It should be possible to have another way. Maybe I will try to disable ChangeTracking or use usings for each and every action.

Thanks a lot. I will let you know tomorrow which solution I chose.

Hmm, I am receiving the error although I have

using(var db = new ApplicationDbContext){
    ...
}

around it ... any ideas?

david
  • 859
  • 1
  • 13
  • 23

1 Answers1

0

I don't know why, but it seems, that there really are references by other ApplicationDbContexts. Since I don't like the (complete) static approach, I am now creating an ApplicationDbContext per Request as posted here: https://stackoverflow.com/a/10153406/1636718

Thanks for your help!

Community
  • 1
  • 1
david
  • 859
  • 1
  • 13
  • 23