使用实体框架,而忽略了一些特性更新实体 [英] Update an entity using entity framework while ignoring some properties

查看:181
本文介绍了使用实体框架,而忽略了一些特性更新实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟asp.net的MVC 4.工作我有使用编辑方法来更新我的持久存储,但我想忽略某些列。

I am working with asp.net mvc 4. I have to update my persistence store using an edit method, but I want to ignore some columns.

我已经在这里找到了一些答案,但他们并没有为我工作(我想)。

I had found some answers here, but they weren't working for me (I suppose).

下面是我的方法:

[HttpPost]
public ActionResult Edit(Candidat candidat)
{
    ProcRecDB _db = new ProcRecDB();  //  from DbContext

    if (ModelState.IsValid)
    {

        _db.Entry(candidat).State = EntityState.Modified;
        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(candidat);
}

候选模型有10个属性;我怎么会忽略其中的一些?

The Candidate model has 10 properties; how would I ignore some of them?

推荐答案

如果您使用EF 5,您可以标记一个属性作为修改了其被标记为后不修改

If your using EF 5, you can mark a property as not modified after its been marked as modified

_db.Entry(candidat).State = EntityState.Modified;
// Ignore changes to the value of SomeProperty
_db.Entry(candidat).Property("SomeProperty").IsModified = false;
_db.SaveChanges();

这篇关于使用实体框架,而忽略了一些特性更新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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