什么是DBContext.Entry吗? [英] What does the DBContext.Entry do?

查看:1751
本文介绍了什么是DBContext.Entry吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[HttpPost]
public ActionResult Edit(Movie movie)
{
    if (ModelState.IsValid)
    {
        db.Entry(movie).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(movie);
}

这个动作接收数据库中的电影模式和更新它。结果
但我无法弄清楚如何。结果
电影对象未连接到数据库,那么,如何实体框架知道哪一行的分贝应该updaed?

This action receives a movie model and updates it in the database.
But I can't figure out how.
The movie object isn't attached to the db, so how does entity framework know which row in the db should be updaed?

我相信,输入方法有什么用它做的,但我真的不知道这是什么方法做。我读这provies信息,但我不明白如何通过只是改变它成为连接并通过的DbContext 国家 $ C>。

I am sure that the Entry method has something to do with it, but I don't really know what this method does. I read that it provies information but I cannot understand how by just changing the State of an entry it becomes attached and tracked by the DBContext.

推荐答案

只是附加的实体到DataContext,否则你将不得不使用主键来搜索实体,然后编辑值并保存它..

it just attaches the the entity to the dataContext otherwise you will have to search for the entity using the primary key and then edit the value and save it..

如果你有,你知道已经存在于数据库中,但改变可能已经作出,那么你可以告诉附加的实体,并设置其状态为修改的背景下一个实体。
<一href=\"http://msdn.microsoft.com/en-US/data/jj592676\">http://msdn.microsoft.com/en-US/data/jj592676

If you have an entity that you know already exists in the database but to which changes may have been made then you can tell the context to attach the entity and set its state to Modified. http://msdn.microsoft.com/en-US/data/jj592676

这篇关于什么是DBContext.Entry吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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