无法访问已处置的对象。 .NET核心

时间:2018-06-02 20:00:20

标签: c# asp.net-core entity-framework-core

我在这一行得到了这个错误:

create table testmp1
(raw_date varchar2(4000));

insert into testmp1
(raw_date) 
values ('2018-06-02T17:31:55.461Z');

insert into testmp1
(raw_date) 
values ('2018-06-02T17:32:48.042Z');

insert into testmp1
(raw_date) 
values ('2015-04-29T19:51:30.0Z');

commit; 

create table testmp2
(new_date date);

以下是代码:

var removeRole = await _userManager.RemoveFromRolesAsync(applicationUser, roles);

我试过"使用"在" var applicationUser"但仍然没有运气。

任何帮助?

这是我的启动类服务:

public async Task SaveClient(UserViewModel viewModel)
{
    try
    {
        var applicationUser = await DbSet.SingleOrDefaultAsync(x => x.Id == viewModel.Id && !x.Deleted);
        if (applicationUser == null) throw new Exception("User not found. ");

        var roles = await _userManager.GetRolesAsync(applicationUser);

        if (!(await _userManager.IsInRoleAsync(applicationUser, viewModel.Role)))
        {
            var removeRole = await _userManager.RemoveFromRolesAsync(applicationUser, roles);
            var addRole = await _userManager.AddToRoleAsync(applicationUser, viewModel.Role);

            if (viewModel.Role.Equals("Agente") && applicationUser.AgentId == null)
                viewModel.AgentId = "A" + new Random().Next(999) + new Random().Next(999);
        }

        Mapper.Map(viewModel, applicationUser);

        await Edit(applicationUser);

        // return Task.CompletedTask;
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message + " User not found. ");
    }
}

1 个答案:

答案 0 :(得分:1)

我最后修改了它:

var applicationUser = await DbSet.SingleOrDefaultAsync(x => x.Id == viewModel.Id && !x.Deleted);
 if (applicationUser == null) throw new Exception("User not found. ");

对此:

var applicationUser = _userManager.FindByIdAsync(viewModel.Id).Result;
if (applicationUser == null || applicationUser.Deleted) throw new Exception("User not found. ");

还从使用 async / await 切换到结果

以下是整个代码:

public Task SaveClient(UserViewModel viewModel)
    {
        try
        {
            var applicationUser = _userManager.FindByIdAsync(viewModel.Id).Result;
            if (applicationUser == null || applicationUser.Deleted) throw new Exception("User not found. ");

            var role = _userManager.GetRolesAsync(applicationUser).Result.FirstOrDefault();

            if (role != viewModel.Role)
            {
                var removeRole = _userManager.RemoveFromRoleAsync(applicationUser, role).Result;
                var addRole = _userManager.AddToRoleAsync(applicationUser, viewModel.Role).Result;

                if (viewModel.Role.Equals("Agente") && applicationUser.AgentId == null)
                    viewModel.AgentId = "A" + new Random().Next(999) + new Random().Next(999);

            }

            Mapper.Map(viewModel, applicationUser);

            Edit(applicationUser);

            return Task.CompletedTask;

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message + " User not found. ");
        }
    }

我希望这可以帮助那些在另一个帖子上找不到答案的人。