gpt4 book ai didi

c# - 使用 Entity Framework 5 错误和 DbUpdateConcurrencyException 更新记录

转载 作者:太空宇宙 更新时间:2023-11-03 10:52:56 26 4
gpt4 key购买 nike

在 MVC 3 Entity Framework 项目中,我似乎在追逐自己的尾部,没有得到要更新的简单记录,这让我发疯了。我错过了什么?

我可以很好地创建记录,但似乎无法计算出执行更新的逻辑。我尝试了多种方法,附加,不附加,并且我认为在涉及外键或其他内容时我错过了一种模式。

View 返回的对象已完全填充且模型有效。填充了外键 ID 字段。

请有任何想法。

POCO 是

public class EFormApplication
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ApplicationId { get; set; }
[ForeignKey("ApplicationTemplateId")]
public virtual ApplicationTemplate ApplicationTemplate { get; set; }
public int ApplicationTemplateId { get; set; }
[Required(ErrorMessage = "Each application must have a Title")]
public string Title { get; set; }
[Required(ErrorMessage = "Each application must have a Description. This is visible to the clients")]
public string Description { get; set; }
[ForeignKey("SecurityId")]
public virtual Security Security { get; set; }
public int SecurityId { get; set; }
public bool Published { get; set; }
[Display(Name = "EIDV Required")]
public bool EIDVRequired { get; set; }
[Display(Name = "Investor confirmation required")]
public bool InvestorConfirmationRequired { get; set; }
[Display(Name = "Advisor confirmation required")]
public bool AdvisorConfirmationRequired { get; set; }
[Display(Name = "Payment required")]
public bool PaymentRequired { get; set; }
[Display(Name = "Login Required")]
public bool UserLoginRequired { get; set; }
[Display(Name = "ISA Trans allowed")]
public bool IsaTransfersAllowed { get; set; }
public DateTime? Deadline { get; set; }
[Display(Name = "Fully Subscribed")]
public bool FullySubscribed { get; set; }
[Display(Name = "Image Url")]
[RegularExpression(@"^(ht)tp(s?):\/\/[0-9a-zA-Z].+$", ErrorMessage = "You must enter a valid URI including the http:// or https://")]
public string ImageUrl { get; set; }
[Display(Name = "Information Url")]
[RegularExpression(@"^(ht)tp(s?):\/\/[0-9a-zA-Z].+$", ErrorMessage = "You must enter a valid URI including the http:// or https://")]
public string InformationUrl { get; set; }
public bool Discontinued { get; set; }
public DateTime Created { get; set; }
public string CreatedBy { get; set; }
public DateTime LastUpdated { get; set; }
public string LastUpdatedBy { get; set; }

//navigation
public virtual ICollection<ApplicationSubmission> ApplicationSubmissions { get; set; }
}

Edit Controller 方法是

    [Authorize(Roles = "Admins")]
public ActionResult Edit(int applicationId)
{
EFormApplication eformapplication = _formRepository.GetEFormApplication(applicationId);
return View(eformapplication);
}


[Authorize(Roles = "Admins")]
[HttpPost]
public ActionResult Edit(EFormApplication eformapplication)
{
if (ModelState.IsValid)
{
eformapplication.LastUpdated = DateTime.UtcNow;
eformapplication.LastUpdatedBy = User.Identity.Name;

var opstatus = _formRepository.UpdateEFormApplication(eformapplication);

if (opstatus.Status)
{
return RedirectToAction("Design", "eForm");
}
else
{
return RedirectToAction("OpStatusError", "Error", opstatus);
}
}

return View(eformapplication);
}

存储库方法UpdateEFormApplication是保存记录的位,如下所示:

    public OperationStatus UpdateEFormApplication(EFormApplication eFormApplication)
{
DataContext.Entry(eFormApplication).State = EntityState.Modified;
DataContext.SaveChanges();

return Save(eFormApplication);
}

最佳答案

当我写这个问题并检查我的事实时,我发现了我犯的一个非常简单的错误!

我忘记在编辑页面上包含 ID 字段,所以在发布的模型中它被设置为零!

教训是......确保所有字段都在表单上表示,如果您不想显示它,则将其作为隐藏字段!

感谢您对 Stack Overflow 的帮助;-)

关于c# - 使用 Entity Framework 5 错误和 DbUpdateConcurrencyException 更新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20700151/

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