【问题标题】:MVC5/EF6: Object cannot be deleted because it was not found in the ObjectStateManager?MVC5/EF6:无法删除对象,因为它在 ObjectStateManager 中找不到?
【发布时间】:2014-06-17 19:19:12
【问题描述】:

我有以下 HttpPost Delete() 方法,该方法在我的 MVC5 应用程序中运行。据我所知,与此控制器、视图甚至模型相关的任何内容都没有改变。

    // POST: Admin/UserManagement/Delete/5
    [HttpPost, ActionName("DeleteConfirmed")]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> DeleteConfirmed(string id)
    {
        ApplicationUser applicationUser = db.Users.Find(id);
        if (applicationUser == null)
        {
            ModelState.AddModelError("", "Failed to find User ID for deletion.");
        }
        else
        {
            IdentityResult result = await UserManager.DeleteAsync(applicationUser);
            if (result.Succeeded)
            {
                await db.SaveChangesAsync();
                return RedirectToAction("Index", "UserManagement");
            }
            else
            {
                ModelState.AddModelError("", "Failed to Delete User.");
                var errors = string.Join(",", result.Errors);
                ModelState.AddModelError("", errors);
            }
        }

        return View(applicationUser);
    }

UserManager 件数:

[CustomAuthorization(myRoles = "Admin")]
public class UserManagementController : Controller
{
    protected ApplicationDbContext db { get; set; }
    private ApplicationUserManager _userManager;

    public UserManagementController()
    {
        this.db = new ApplicationDbContext();
    }

    public UserManagementController(ApplicationUserManager userManager)
    {
        UserManager = userManager;
    }

    public ApplicationUserManager UserManager
{
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

当我的代码到达 IdentityResult result = await UserManager.DeleteAsync(applicationUser) 时,它会立即跳到下面的 Dispose() 方法,而不是加载索引视图,而是给出:Server Error in '/' Application. The object cannot be deleted because it was not found in the ObjectStateManager.

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();

            if (UserManager != null)
            {
                UserManager.Dispose();
                UserManager = null;
            }
        }
        base.Dispose(disposing);
    }

谁能告诉我哪里出错了?我以前从未见过这个错误。以前,此 DeleteConfirmed() 代码完全按预期工作。

编辑:

Startup.cs:

using Microsoft.Owin;
using Owin;

[assembly: OwinStartupAttribute(typeof(PROJECT.Startup))]
namespace PROJECT
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}

EDIT2:

CustomAuthorization.cs(助手):

public class CustomAuthorization : AuthorizeAttribute
{
    public string myRoles { get; set; }

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        var userAuthInfo = HttpContext.Current.User;

        if (userAuthInfo != null)
        {
            if (!userAuthInfo.Identity.IsAuthenticated)
            {
                string returnUrl = filterContext.HttpContext.Request.RawUrl;
                filterContext.Result = new RedirectResult("/Account/Login?returnUrl=returnUrl");
                return;
            }

            string[] roles = myRoles.Split(',');


            var userAuth = false;

            foreach (string role in roles)
            {

                if (userAuthInfo.IsInRole(role))
                {
                    userAuth = true;
                    break;
                }
            }

            if (!userAuth)
            {
                var result = new RedirectResult("/Home?auth=0");

                filterContext.Result = result;
            }
        }
    }
}

EDIT3

/App_Start/ 中的 Startup.Auth.cs:

public partial class Startup
{
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context and user manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    }
}

解决方案 正如 haim770 指出的那样,我在 DeleteConfirmed() 操作方法中错误地使用了 2 个 DbContext 引用。

将 ApplicationUser applicationUser = db.Users.Find(id) 更改为正确的 async 并且一切正常:ApplicationUser applicationUser = await UserManager.FindByIdAsync(id);

感谢所有花时间提供帮助的人!

【问题讨论】:

  • db 是从哪里来的?它是如何实例化的? UserManager 是否共享相同的 db?
  • 抱歉,添加到 UserManager 中
  • 你的Startup.cs看起来怎么样?
  • 看起来ApplicationUserManager 没有使用与控制器相同的ApplicationDataContext?
  • 添加了Startup.cs。我正在使用 Asp.Net Identity。

标签: c# asp.net-mvc asp.net-mvc-5 entity-framework-6 objectstatemanager


【解决方案1】:

问题是您实际上在这里使用了 2 DbContext 并且它们无法跟踪彼此的实体。

您必须更改控制器代码以使 db 和 UserManager 共享相同的上下文实例引用:

public UserManagementController()
{
    this.db = context.Get<ApplicationDbContext>();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-07
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多