无法删除该对象,因为删除项目时在ObjectStateManager错误中找不到该对象 [英] The object cannot be deleted because it was not found in the ObjectStateManager error when deleting item

查看:173
本文介绍了无法删除该对象,因为删除项目时在ObjectStateManager错误中找不到该对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从我的AspNetRoles表中删除一项已扩展为以下内容的项目:

I am trying to delete an item from my AspNetRoles table that has been extended like this:

public class AspApplicationRoles : IdentityRole
{
    public AspApplicationRoles() : base() { }
    public AspApplicationRoles(String Name) : base(Name) { }
    [Required]
    public String ApplicationId { get; set; }
    public AspNetApplications Application { get; set; }
}

这是我尝试删除它的方法.我使用带有ID输入的Get函数来获取项目并尝试将其删除.但是,我在manager处收到此错误.删除: {由于在ObjectStateManager中找不到该对象,因此无法删除该对象."}

This is the way I am trying to delete it. I use the Get function with an Input of its ID to get the item and try to delete it. However, I get this error at manager.Delete: {"The object cannot be deleted because it was not found in the ObjectStateManager."}

    public RolesDetailsViewModel Get(String Input)
    {
        AspApplicationRoles data = new AspApplicationRoles() ;
        RolesDetailsViewModel rdvm = new RolesDetailsViewModel();
        try
        {
            data = dbContext.AspApplicationRoles.Single(r => r.Id == Input);
            rdvm = new RolesDetailsViewModel
            {
                ApplicationId = data.ApplicationId,
                Id = data.Id,
                RoleName = data.Name
            };
        }
        catch (Exception e)
        {
            logger.Error(e, AspNetEventLogs.NotFound);
        }
        return rdvm;
    }

    public void Delete(String Input)
    {
        var store = new RoleStore<AspApplicationRoles>(dbContext);
        var manager = new RoleManager<AspApplicationRoles>(store);
        try
        {
            var role = Get(Input);
            var r = new AspApplicationRoles
            {
                Id = role.Id,
                ApplicationId = role.ApplicationId,
                Name = role.RoleName
            };
            manager.Delete(r);
            logger.Info(AspNetEventLogs.Delete + " Role: " + role.RoleName);
        }
        catch (Exception e)
        {
            logger.Error(e, "Failed to delete Role");
        }
    }

我尝试寻找一种解决方案,但是如果使用dbContext.Attach函数,则会收到另一个错误,指出存在两个具有相同主键的对象?

I have tried looking around for a solution but if I use the dbContext.Attach function, I get another error that states that there are 2 objects with the same primary key?

希望有人可以看到这个.谢谢.

Hope someone can see this. Thank you.

当我将Delete函数更改为此时,我的代码有效:

My code works when I change my Delete function to this:

    public void Delete(String Input)
    {
        var store = new RoleStore<AspApplicationRoles>(dbContext);
        var manager = new RoleManager<AspApplicationRoles>(store);
        try
        {
            var role = dbContext.AspApplicationRoles.Single(r => r.Id == Input);
            manager.Delete(role);
            logger.Info(AspNetEventLogs.Delete + " Role: " + role.Name);
        }
        catch (Exception e)
        {
            logger.Error(e, "Failed to delete Role");
        }
    }

但是,有可能重用我的Get函数而不是每次都调用LINQ函数吗?

However, is it possible to reuse my Get function instead of calling the LINQ function each time?

推荐答案

RoleManager.Delete 调用 RoleStore.Delete ,最终调用 DbSet< Role>.删除(角色).正如 DbSet< T> .Remove(T) ,则在调用此方法之前,您传递给实体的对象必须以其他某种状态存在于上下文中.

RoleManager.Delete calls RoleStore.Delete which finally call DbSet<Role>.Remove(role). As it's already mentioned in DbSet<T>.Remove(T), the that you pass to the entity must exist in the context in some other state before this method is called.

显然,您使用 new Role()创建的角色实例在上下文中不存在.因此,以下代码行将引发异常:

Obviously the role instance which you create using new Role() doesn't exists in the context. So the following line of code will throw an exception:

var r = new Role(){ ... };
manager.Delete(r); // ← throws exception

上面的描述还描述了为什么您编辑的代码起作用的原因,因为您将角色加载到上下文中,然后尝试删除该附加的(跟踪的)角色.

Above description also describes why your edited code works, because you load a role into context, then try to delete that attached (tracked) role.

如果您想重复使用创建的 Get 方法,则该方法应返回 Role 或在其中引用 Role 返回值.然后,该引用(已附加到EF上下文中)可以被管理员轻松删除,因为已对其进行了跟踪.

If you would like to reuse the Get method which you create, the method should return Role or have a reference to the Role in the return value. Then that reference (which has been attached to EF context) can be easily deleted by manager, because it's been tracked.

这篇关于无法删除该对象,因为删除项目时在ObjectStateManager错误中找不到该对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆