ASP.NET MVC3 code-第一错误尝试更新实体 [英] ASP.NET MVC3 Code-First Error- attempting to update entity

查看:108
本文介绍了ASP.NET MVC3 code-第一错误尝试更新实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查<一href=\"http://stackoverflow.com/questions/4608734/the-conversion-of-a-datetime2-data-type-to-a-datetime-data-type-resulted-in-an-ou\">this问题它似乎与我所需要的,但并不完全回答。

I've checked this question and it seems to be related to what I need, but does not answer it exactly.

我有一个实体(使用SQL精简EF code首先通过MVC3-如果这还从标题清)一个问题(通用跟踪问题,只是如何MVC3作品我自己理解的教育)。问题类有一个CreatedBy属性(智力参考谁创建此问题的用户)和CreatedDate属性(日期时间)。当我使用脚手架code更新(从用户正在修改修改才prevent一些更新的日期字段):

I have an entity (Sql Compact using EF Code First via MVC3- if that wasn't clear from the title) for an "Issue" (generic issue tracking, just for my own education understanding of how MVC3 works). The Issue class has a CreatedBy property (Int reference to a User who Created the Issue) and a CreatedDate property (DateTime). When I use the scaffolded code to update (modified only to prevent some updated date fields from being modified by the user):

        if (ModelState.IsValid)
        {
            issue.LastActivity = (DateTime?)DateTime.Now.Date;
            if (issue.ClosedBy != null) issue.ClosedDate = (DateTime?)DateTime.Now.Date;
            startingIssue = null;
            db.Entry(issue).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

我收到链接的问题(一DATETIME2数据类型的转换为datetime数据类型等,等,)

I receive the error mentioned in the linked question (conversion of a datetime2 data type to a datetime data type etc., etc.,)

当我通过code步骤,它出现在我的CreatedBy和CreatedDate属性不包含在问题控制器被绕过的实例。当我尝试从DB抓住了问题的另一个副本,并更新那些值来解决这个问题:

When I step through the code, it appears my CreatedBy and CreatedDate properties are not contained in the instance of issue that the controller is passing around. When I try to fix that by grabbing another copy of the issue from the db, and updating those to values:

        var startingIssue = db.Issues.Find(issue.IssueId);
        if (ModelState.IsValid)
        {
            if (issue.CreatedBy != startingIssue.CreatedBy) issue.CreatedBy = startingIssue.CreatedBy;
            if (issue.CreatedDate != startingIssue.CreatedDate) issue.CreatedDate = startingIssue.CreatedDate;
            issue.LastActivity = (DateTime?)DateTime.Now.Date;
            if (issue.ClosedBy != null) issue.ClosedDate = (DateTime?)DateTime.Now.Date;
            startingIssue = null;
            db.Entry(issue).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

我得到的并发冲突:具有相同键的对象已经存在于ObjectStateManager。该ObjectStateManager无法跟踪多个对象使用相同的密钥。

I get the concurrency violation: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

所以,我怎么EF看到这已经是在DB设置日期(所以它不会尝试的CreatedDate更新到1/1/0001)在不违反并发?

So, how do I get EF to see the date which is already set in the DB (so it doesn't try to update the CreatedDate to 1/1/0001) without violating concurrency?

修改
好吧...我发现它。我是,很显然,寻找 @ Html.HiddenFor(型号=方式&gt;模型[属性])反正添加编辑器视图。这似乎有点傻,有点迂回的我,但它的工作,而无需添加自定义code分离一个对象,并替换为一个更新。

Edit Okay... I found it. I was, apparently, looking for @Html.HiddenFor(model => model.[property]) and adding the editor to the view anyway. That seems a little silly and round-about to me, but it does work without having to add custom code to detach one object and substitute an updated one.

推荐答案

简短的回答是,你已经加载了实体与上下文中的查找,你不能在以后附加一个又一个。

The short answer is that you've already loaded the entity into the context with the Find and you cannot later attach another one.

您只剩下两个选择:


  • 分离初审,然后装上第二个

  • 从二审复制领域的第一个

我将分享code为第一个选项。首先,添加一个分离方法的的DbContext 实施

I'll share code for the first option. First, add a Detach method to your DbContext implementation:

public void Detach(object entity)
{
    var objectContext = ((IObjectContextAdapter)this).ObjectContext;
    objectContext.Detach(entity);
}

然后调用分离而不是变量设置为

var startingIssue = db.Issues.Find(issue.IssueId);
if (ModelState.IsValid)
{
    if (issue.CreatedBy != startingIssue.CreatedBy) issue.CreatedBy = startingIssue.CreatedBy;
    if (issue.CreatedDate != startingIssue.CreatedDate) issue.CreatedDate = startingIssue.CreatedDate;
    issue.LastActivity = (DateTime?)DateTime.Now.Date;
    if (issue.ClosedBy != null) issue.ClosedDate = (DateTime?)DateTime.Now.Date;

    // startingIssue = null;
    db.Detach(startingIssue);

    db.Entry(issue).State = EntityState.Modified;
    db.SaveChanges();
    return RedirectToAction("Index");
}

这篇关于ASP.NET MVC3 code-第一错误尝试更新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆