1

I have ASP.Net Core 2.1 application & using EF Core 2.1 as DAL

This is my how DAL service method looks.

public async Task<bool> UpdateChannels(IEnumerable<Channels> subscriptions)
{
    try
    {
        //here Context(MyReosiptory =DbContext) no issue
        var zoneChannels = await Context.Channnels.Where(x => x.isActive == true).ToListAsync();
        await Context.Channels.AddRangeAsync(subscriptions);
        //but at this line it throws the error.
        await Context.SaveChangesAsync();
        return true;
    }   
    catch (Exception ex)
    {
        throw new Exception(ex.Message ?? ex.InnerException.Message);
    }
}

However, if I move the other Db Operation (add) in separate method, it runs.

But I have one more Db operation in the same method, & cannot move each Db related operation to separate method.

services.AddScoped<IMyRepository, MyRepository>();

Injecting the Dependency via Constructor.

Checked the below link.

Cannot access a disposed object in ASP.NET Core when injecting DbContext

But neither the Seed() is coming up nor do I understand why I should have Seed() & how it is related to disposed object.

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • I had the same issue but I do not remember if I did something else beside I added to db connection string the option `MultipleActiveResultSets=true` – Gabriel Costin Apr 08 '19 at 07:06
  • is your catch block outside the method? – Priyesh Kumar Apr 08 '19 at 07:12
  • @Fabio, I have mentioned in comments in snippet – Kgn-web Apr 08 '19 at 07:23
  • How you are registering DbContext in DI services? – Fabio Apr 08 '19 at 07:25
  • 1
    I expect you might be missing a call to `await` somewhere? e.g. Are you `await`ing the call to `UpdateChannels`? – Kirk Larkin Apr 08 '19 at 07:29
  • Please create an [MCVE](https://stackoverflow.com/help/mcve) that includes all exception details including stack traces of the exception and all inner exceptions. Your current post is not an MCVE and it is hard (if not impossible) for anyone to answer your question. – Steven Apr 08 '19 at 10:19
  • Where is `Context` coming from? Is that instance of data context being passed around between objects at all? Does anything else use the same instance? We need to see more layers of this code to be able to help – Arcanox Apr 08 '19 at 16:21

0 Answers0