gpt4 book ai didi

c# - 存储更新、插入或删除语句影响了意外的 .自加载实体后修改或删除的实体

转载 作者:行者123 更新时间:2023-11-30 14:53:54 31 4
gpt4 key购买 nike

我正在为我的项目使用 MVC。

我添加了一个名为 group 的 Controller ,在这个 Controller 中我有一些像往常一样的操作,比如 createedit 等等。

但我的问题是指 edit 方法正如你在这里看到的:

public ActionResult Edit(int GroupId)
{
ViewBag.groupIdLST = new SelectList(OBJgroupRepository.FindBy(i => i.GroupId == null).ToList(), "Id",
ViewBag.GroupType = new SelectList(OBJgroupRepository.FindBy(i => i.GroupId == null).ToList(), "name",
DomainClass.Group tg = OBJgroupRepository.FindBy(i => i.Id == GroupId).First();
return View(tg);
}
[HttpPost]
public ActionResult Edit(Group gr)
{
if (ModelState.IsValid)
{
OBJgroupRepository.Edit(gr);
OBJgroupRepository.Save();
}
TempData["success"] = "اطلاعات با موفقیت ویرایش شد";
return RedirectToAction("Create");
}

当我点击编辑按钮时,出现了这个错误:

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.

我的编辑保存方法:

public virtual void Edit(T entity)
{
_entities.Entry(entity).State = System.Data.Entity.EntityState.Modified;
}

public virtual void Save()
{
try
{
_entities.SaveChanges();
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
}

我的仓库:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DomainClass;
using ProjectModel;

namespace Repository
{
public class GroupRepository : GenericRepository<DataAccessModelContainer, Group>
{
}
}

可根据要求提供任何详细信息。

最好的问候。

最佳答案

问题出在这里:

OBJgroupRepository.Edit(gr);
OBJgroupRepository.Save();

您调用了 OBJgroupRepository 两次!这会导致线程之间的竞争条件(以及随后的并发性考虑)。 尝试在编辑中使用保存方法内容。

关于c# - 存储更新、插入或删除语句影响了意外的 .自加载实体后修改或删除的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28263092/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com