【发布时间】:2016-07-17 10:20:33
【问题描述】:
我正在为我的项目使用 MVC。
我添加了一个名为 group 的控制器,在这个控制器中我有一些像往常一样的操作,例如 create 和 edit 等等。
但我的问题是指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");
}
当我点击编辑按钮时出现此错误:
存储更新、插入或删除语句影响了意外 行数 (0)。实体可能已被修改或删除 实体已加载。看 http://go.microsoft.com/fwlink/?LinkId=472540 获取有关信息 理解和处理乐观并发异常。
我的编辑和保存方法:
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>
{
}
}
可根据要求提供任何详细信息。
最好的问候。
【问题讨论】:
-
谢谢@AmirHosseinMehrvarzi 我想知道为什么会发生这个错误?
标签: c# asp.net-mvc entity-framework asp.net-mvc-4