c# - 失败,因为同一类型的另一个实体已经具有相同的主键值 As
问题描述
如何修复错误..
当更新记录显示此错误时:
显示错误:{“附加类型为“DomainClass.WorkshopReport”的实体失败,因为同一类型的另一个实体已经具有相同的主键值。使用“附加”方法或将实体的状态设置为“时可能会发生这种情况”如果图中的任何实体具有冲突的键值,则为“未更改”或“已修改”。这可能是因为某些实体是新的,尚未收到数据库生成的键值。在这种情况下,请使用“添加”方法或“添加”实体状态来跟踪图表,然后根据需要将非新实体的状态设置为“未更改”或“已修改”。"}
域类:
namespace DomainClass
{
public class WorkshopReport
{
public int Id { set; get; }
public int UserId { set; get; }
public int? ManagerIdConfirm { set; get; }
public bool? ManagerConfirmState { set; get; }
public DateTime? ManagerConfirmDateTime { set; get; }
public int? SuperviderIdConfirm { set; get; }
public bool? SuperviderConfirmState { set; get; }
public DateTime? SuperviderConfirmDateTime { set; get; }
public string ReportNumber { set; get; }
public string Shift { set; get; }
public string ShiftWork { set; get; }
public DateTime ShiftDate { set; get; }
public string ShiftPersennel { set; get; }
public string Type { set; get; }
public DateTime SubmitDateTime { set; get; }
}
}
接口库:
namespace InterfaceRepository
{
public interface IWorkshopReportRepository
{
IQueryable<WorkshopReport> Get();
bool Save();
bool Add(WorkshopReport newValue);
bool Delete(WorkshopReport deleted);
bool Edit(WorkshopReport updated);
IQueryable<WorkshopReport> FindById(int Id);
IQueryable<WorkshopReport> Search(string ReportNumber);
}
}
存储层:
namespace RepositoryLayer
{
public class WorkshopReportRepository:IWorkshopReportRepository
{
public CMSDataContext _ctx;
public WorkshopReportRepository(CMSDataContext ctx)
{
_ctx = ctx;
}
public IQueryable<WorkshopReport> Get()
{
return _ctx.WorkshopReports;
}
public bool Save()
{
try
{
return _ctx.SaveChanges() > 0;
}
catch (Exception ex)
{
// TODO log this error
return false;
}
}
public bool Add(WorkshopReport newValue)
{
try
{
_ctx.WorkshopReports.Add(newValue);
return true;
}
catch (Exception ex)
{
// TODO log this error
return false;
}
}
public bool Delete(WorkshopReport deleted)
{
try
{
_ctx.WorkshopReports.Remove(deleted);
return true;
}
catch (Exception ex)
{
// TODO log this error
return false;
}
}
public bool Edit(WorkshopReport updated)
{
try
{
_ctx.Entry(updated).State = System.Data.Entity.EntityState.Modified;
return true;
}
catch (Exception ex)
{
// TODO log this error
return false;
}
}
public IQueryable<WorkshopReport> Search(string Value)
{
return _ctx.WorkshopReports.Where(i => i.ReportNumber.Contains(Value));
}
public IQueryable<WorkshopReport> FindById(int Id)
{
return _ctx.WorkshopReports.Where(i => i.Id == Id);
}
}
}
车间控制器:
public ActionResult Edit(WorkshopReport value, FormCollection formvalue)
{
try
{
value.ShiftDate =
_calenderRepository.ConvertPersianToEnglishFormat(formvalue["ShiftDate"].ToString());
value.SubmitDateTime=DateTime.Now;
value.Type = internalType;
value.UserId= _userRepository.FindByEmail(User.Identity.Name).Id;
if (_workshopReportRepository.Edit(value))
{
_workshopReportRepository.Save();
TempData["Success"] = "Updated";
}
}
catch (Exception)
{
TempData["Error"] = "Try again...";
}
return RedirectToAction("Index", "WorkshopReport", new { type = internalType });
解决方案
问题就这样解决了。
public bool Edit(WorkshopReport updated)
{
try
{
var local = _ctx.Set<WorkshopReport>()
.Local
.FirstOrDefault(f => f.Id == updated.Id);
if (local != null)
{
_ctx.Entry(local).State = EntityState.Detached;
}
_ctx.Entry(updated).State = System.Data.Entity.EntityState.Modified;
return true;
}
catch (Exception ex)
{
// TODO log this error
return false;
}
}
推荐阅读
- ckan - #ckan 数据浏览器
- c# - C#将对象列表写入文件(最简单的方法)
- android - android gradle //无检查 GradleCompatible
- node.js - 如何在 jest nodejs 中配置`jest-html-reporter`
- excel - 为 VBA 组合框创建动态范围
- java - 来自 Maven 配置文件的 context.xml 的不同资源
- c# - 使用 iText 7 C# 在段落后画线
- php - 如何使用php将数据从表单输入到mysql?
- db2 - 用户定义函数的临时表
- docusignapi - 设置完成证书作为附件发送给 Docusign 签名过程中的每个接收者