English 中文(简体)
尝试更新实体时出错
原标题:Error when trying to update an entity

我在MVC 3应用程序中使用实体框架4.3,

存储更新、插入或删除的语句影响数量出意外的行数(0)

当我进入调试模式时,我在[HttpPost]方法上看到,没有提供种子代号:

public ActionResult Edit(Feed feed)
    {
        if (ModelState.IsValid)
        {
            db.Entry(feed).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.FolderId = new SelectList(db.Folders, "FolderId", "Name", feed.FolderId);
        return View(feed);
    }

这些是我的实体

种子 :

public class Feed
{
    [ScaffoldColumn(false)]
    public int FeedId { get; set; }

    [StringLength(150, ErrorMessage = "The {0} must be less then {1} charecters")]
    public string Title { get; set; }

    [ScaffoldColumn(false)]
    public string Description { get; set; }

    [Required(ErrorMessage = "you must enter a valid link")]
    [StringLength(500, ErrorMessage = "The {0} must be less then {1} characters long.")]
    public string LinkUrl { get; set; }

    [ScaffoldColumn(false)]
    public DateTime PublishDate { get; set; }

    [ScaffoldColumn(false)]
    public string Image { get; set; }

    [ForeignKey("Folder")]
    [Required(ErrorMessage="you must choose a folder")]
    public int FolderId { get; set; }

    public virtual Folder Folder { get; set; }

    public Feed()
    {
        PublishDate = new DateTime(2012, 1, 1);
    }
}

文件夹 :

public class Folder
{    
    public int FolderId { get; set; }

    [Required(ErrorMessage = "you must enter a folder name")]
    [StringLength(150, ErrorMessage = "the {0} must be less then {1} charecters")]
    public string Name { get; set; }
}

我找了个解决办法,但是他们都没有像尝试刷新方法那样工作, DbContext 中不存在这种方法, 或者在 FeedId 和 FatrickId 上方定义一个[ Key] 属性 。

问题回答

您不应该手动维护实体国家, 更改跟踪应该根据上下文进行 。

您正在使用查看模型,并假定该模型应附在数据库中。

你需要做一些类似..

Feed DbFeed = DBSet.Where(f => f.id = feed.Id);
DbFeed.Property = NewValue;
db.SaveChanges();

(排除可能不正确的语法 - 我默认在 VB 工作)

也就是说, 从 DB 上下文中获取 Feed 对象的新实例, 然后对您给定的对象进行修改 。

这是因为上下文实际上没有给您一个普通的 < code> Feed 对象, 而是一个匿名类型, 包住它, 并且具有相同的属性。 包件覆盖了您的方法, 检测到属性的变化, 也就是它如何保持状态 。

您从视图中获取回来的种子对象不包含此包装, 因此有问题

实体框架跟踪对象, 从您视图获得的种子未跟踪 。 此状态的模式是从 db 获取您想要更新的对象, 然后调用更新模式, 它将将您未跟踪的实体的更改应用到您的跟踪实体, 您可以保存它 。

    if (ModelState.IsValid)
    {
        var trackedEntity = db.Find(feed.Id)
        UpdateModel(trackedEntity);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

显然,但将[ScoffoldColumn(false)]属性放入我的模型,在我看来,它并没有创造它,而对于id则没有通过。

我在我的模型中添加了 , 并且解决了这个问题 。

Apparently you were having concurrency problems. Your update state should be running as:

   UPDATE tableA SET colA =  value  WHERE colX1 =  compare1  AND colX2 =  compare2 ;

colXn 可以是您的主要密钥等,也可以是您正在使用的每列密钥等。 如果您没有处理同一种货币, 如果某人得到的数据与您相同的时间, 在您之前更改并保存它, 您的 WHERE 语句将永远无法匹配, 因为您正在更新的记录信息已经有新信息了 。





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签