Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在更新时忽略某些列_C#_Asp.net Mvc_Entity Framework_Ef Database First - Fatal编程技术网

C# 在更新时忽略某些列

C# 在更新时忽略某些列,c#,asp.net-mvc,entity-framework,ef-database-first,C#,Asp.net Mvc,Entity Framework,Ef Database First,你好,我有这样的东西: public ActionResult Edit(int id) { var movie = (from m in _db.Movies where m.Id == id select m).First(); return View(movie); } [HttpPost] public ActionResult Edit(Movie movie) { try { var originalMovie = (from m

你好,我有这样的东西:

public ActionResult Edit(int id)
{
    var movie = (from m in _db.Movies where m.Id == id select m).First();

    return View(movie);
}

[HttpPost]
public ActionResult Edit(Movie movie)
{
    try
    {
        var originalMovie = (from m in _db.Movies where m.Id == movie.Id select m).First();

        _db.Movies.ApplyCurrentValues(movie);

        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}
_db.Movies.Attach(movie);
ObjectStateEntry entryToUpdate = db.ObjectStateManager.GetObjectStateEntry(movie);
entryToUpdate.SetModifiedProperty("field1"); // Replace "field1" with the name of the 1st field to update
entryToUpdate.SetModifiedProperty("field2"); // Replace "field2" with the name of the 2nd field to update
entryToUpdate.SetModifiedProperty("field3"); // Replace "field3" with the name of the 3rd field to update
_db.SaveChanges();
这个例子取自

我只想将修改过的列传递给DB SQL查询(更新电影…),因为我正在执行列审核

代码运行正常,但问题是在我的“Movie”实体中,我有一个“FlagOldMovie”属性和其他10个属性,我在此视图中不使用它们,因为它们将保持不变,但entityframework将该属性设置为默认值,以便“ApplyCurrentValues”查找更改并更新属性

一种解决方法是将未更改的属性传递给html隐藏输入,但传递其私有数据

有什么想法吗?

试试看

var originalMovie = (from m in _db.Movies where m.Id == movie.Id select m).First();

originalMovie.updateme = updating;

_db.SaveChanges();
试试看

var originalMovie = (from m in _db.Movies where m.Id == movie.Id select m).First();

originalMovie.updateme = updating;

_db.SaveChanges();
这将忽略您指定的列,我通常这样做是为了排除像
Id
这样的字段

但是如果你忽略了很多列,那么你应该考虑VIEW模型概念,你只需要一个视图所需的属性。

编辑:还有一些问题吗

下面是如何添加多个

[HttpPost]
public ActionResult Edit([Bind(Exclude ="c_name, c_name2, c_name3")] Movie movie)
{
//code here
}
这将忽略您指定的列,我通常这样做是为了排除像
Id
这样的字段

但是如果你忽略了很多列,那么你应该考虑VIEW模型概念,你只需要一个视图所需的属性。

编辑:还有一些问题吗

下面是如何添加多个

[HttpPost]
public ActionResult Edit([Bind(Exclude ="c_name, c_name2, c_name3")] Movie movie)
{
//code here
}

您可以告诉EF要更新哪些字段。试着这样做:

public ActionResult Edit(int id)
{
    var movie = (from m in _db.Movies where m.Id == id select m).First();

    return View(movie);
}

[HttpPost]
public ActionResult Edit(Movie movie)
{
    try
    {
        var originalMovie = (from m in _db.Movies where m.Id == movie.Id select m).First();

        _db.Movies.ApplyCurrentValues(movie);

        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}
_db.Movies.Attach(movie);
ObjectStateEntry entryToUpdate = db.ObjectStateManager.GetObjectStateEntry(movie);
entryToUpdate.SetModifiedProperty("field1"); // Replace "field1" with the name of the 1st field to update
entryToUpdate.SetModifiedProperty("field2"); // Replace "field2" with the name of the 2nd field to update
entryToUpdate.SetModifiedProperty("field3"); // Replace "field3" with the name of the 3rd field to update
_db.SaveChanges();

您可以告诉EF要更新哪些字段。试着这样做:

public ActionResult Edit(int id)
{
    var movie = (from m in _db.Movies where m.Id == id select m).First();

    return View(movie);
}

[HttpPost]
public ActionResult Edit(Movie movie)
{
    try
    {
        var originalMovie = (from m in _db.Movies where m.Id == movie.Id select m).First();

        _db.Movies.ApplyCurrentValues(movie);

        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}
_db.Movies.Attach(movie);
ObjectStateEntry entryToUpdate = db.ObjectStateManager.GetObjectStateEntry(movie);
entryToUpdate.SetModifiedProperty("field1"); // Replace "field1" with the name of the 1st field to update
entryToUpdate.SetModifiedProperty("field2"); // Replace "field2" with the name of the 2nd field to update
entryToUpdate.SetModifiedProperty("field3"); // Replace "field3" with the name of the 3rd field to update
_db.SaveChanges();

我终于明白了,首先,这个解决方案只适用于.NET4.5+

[HttpPost]
public ActionResult Edit(Movie movie)
{
    try
    {
        //Get DB version
        var originalMovie = (from m in _db.Movies where m.Id == movie.Id select m).First();
        //Mark changes with data received
        _db.Movies.ApplyCurrentValues(movie);

        //CODE ADDED - Ignoring field/properties you dont want to update to DB
        ObjectStateEntry entryToUpdate = db.ObjectStateManager.GetObjectStateEntry(originalMovil);
        entryToUpdate.RejectPropertyChanges("field1");
        entryToUpdate.RejectPropertyChanges("field2");
        entryToUpdate.RejectPropertyChanges("field3");
        //-----------------

        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

在这段代码中,唯一修改的数据是您想要的,接下来我要做的是审计将_db.SaveChanges()扩展为_db.SaveChangesAudito(id)的列

我终于明白了,首先,这个解决方案只适用于.NET4.5+

[HttpPost]
public ActionResult Edit(Movie movie)
{
    try
    {
        //Get DB version
        var originalMovie = (from m in _db.Movies where m.Id == movie.Id select m).First();
        //Mark changes with data received
        _db.Movies.ApplyCurrentValues(movie);

        //CODE ADDED - Ignoring field/properties you dont want to update to DB
        ObjectStateEntry entryToUpdate = db.ObjectStateManager.GetObjectStateEntry(originalMovil);
        entryToUpdate.RejectPropertyChanges("field1");
        entryToUpdate.RejectPropertyChanges("field2");
        entryToUpdate.RejectPropertyChanges("field3");
        //-----------------

        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

在这段代码中,唯一修改的数据是您想要的,接下来我要做的是审计将_db.SaveChanges()扩展为_db.SaveChangesAudito(id)的列

最佳做法是使用ViewModel,而不是域/数据模型传递到视图或从视图传递到视图。:)


这种情况说明了不这样做的危险之一

最佳做法是使用ViewModel,而不是域/数据模型传递到视图或从视图传递到视图。:)


这种情况说明了不这样做的危险之一

对不起,我不明白你的答案,什么是“更新”?啊!顺便说一句,谢谢你的回复,但我在配置中寻找一个标志或东西,告诉EF ignore这是列,而不必重新键入所有代码并保持代码简短清晰,我必须为应用程序中的所有CRUD操作执行此操作,例如,这会暴露给view 20属性。但这是一个公认的方法。对不起,我不明白你的答案,什么是“更新”?啊!顺便说一句,谢谢你的回复,但我在配置中寻找一个标志或东西,告诉EF ignore这是列,而不必重新键入所有代码并保持代码简短清晰,我必须为应用程序中的所有CRUD操作执行此操作,例如,这会暴露给view 20属性。但这是一个被接受的方法。这似乎是我一直在寻找的解决方案,但我不知道为什么不适合我。。。非常感谢。我会试着找出问题所在。什么问题?也许我可以帮助你进一步它不是排除列,我不知道这是因为我要排除的列不可为null,还是因为我的操作比示例稍微复杂一些,比如:public ActionResult Edit(电影,字符串a,int b,int c)我更新了我的问题,尝试显示一些模型和视图,这将有助于alsoBind不是为了这个目的。Bind可防止过度发布攻击,即客户端可以过度发布字段名和值,即“IsAdmin=true”,并且由于默认的模型绑定行为,如果模型具有IsAdmin字段,则即使您在客户端输出中忽略了它,也会将其设置为true,即html中没有用于设置IsAdmin的字段。Bind在数据层上不起任何作用,这是Ops的问题。它所做的一切都忽略了客户端值与实体之间的绑定,因此您仍将获得默认值等发送到数据层,从而输入到数据库中。这似乎是我一直在寻找的解决方案,但我不知道为什么不适合我。。。非常感谢。我会试着找出问题所在。什么问题?也许我可以帮助你进一步它不是排除列,我不知道这是因为我要排除的列不可为null,还是因为我的操作比示例稍微复杂一些,比如:public ActionResult Edit(电影,字符串a,int b,int c)我更新了我的问题,尝试显示一些模型和视图,这将有助于alsoBind不是为了这个目的。Bind可防止过度发布攻击,即客户端可以过度发布字段名和值,即“IsAdmin=true”,并且由于默认的模型绑定行为,如果模型具有IsAdmin字段,则即使您在客户端输出中忽略了它,也会将其设置为true,即html中没有用于设置IsAdmin的字段。Bind在数据层上不起任何作用,这是Ops的问题。它所做的一切都忽略了客户端值与实体的绑定,因此您仍将获得默认值等发送到数据层,从而输入到数据库中。谢谢,我将尝试一下,可以做相反的事情,设置所有属性修改(就像我的代码一样),并使用此coode取消那些我不使用的属性?谢谢,我将尝试一下,可以做相反的事情,设置所有属性modified(就像我的代码一样),然后用这个coode取消设置那些我不使用它的属性?