Asp net core entity framework - Cannot access remote entity

The following code:

public async static Task<List<RecentTask>> GetRecentTasks(ApplicationDbContext db,
    int EmployeeID, int NewTaskID, ILogger<HomeController> logger)
{
    var TaskList = await db.RecentTask.ToListAsync(); // without this line - no exception

    var t = new RecentTask();
    t.EmployeeID = EmployeeID;
    t.TaskDefinitionID = NewTaskID;
    db.RecentTask.Add(t);
    await db.SaveChangesAsync(); // EXCEPTION!

    return TaskList;
}

      

raises an exception:

Unable to access the remote object. A common cause of this error is deleting a context that was resolved by dependency injection and then trying to use the same context instance elsewhere in your application. This can happen if you call Dispose () on the context, or if you wrap the context in a using statement. If you are using dependency injection, you must let the dependency injection container take care of disposing of the context instances. Object name: "ApplicationDbContext".

Removing the first line will make the exception disappear. I don't see how the object db

will be located. Anyone have any ideas what's going on here?

+3


source to share





All Articles