的DbContext,状态和初始值 [英] DBContext, state and original values

查看:187
本文介绍了的DbContext,状态和初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与ASP.NET MVC3使用EF和code第一个工作日。

I'm working with ASP.NET MVC3 using EF and Code First.

我在写练习一个简单的问题跟踪器。在我的控制器我有code的一个相当标准位:

I'm writing a simple issue tracker for practice. In my Controller I have a fairly standard bit of code:

[HttpPost]
public ActionResult Edit(Issue issue) {
    if (ModelState.IsValid) {
        dbContext.Entry(issue).State = EntityState.Modified
        .....
    }
}

问题的一部分1
我试图让我周围的DbContext如何工作的负责人 -
之前我给自己定的dbContext.Entry(问题)的状态下,我认为我的问题对象分离。有一次,我设置状态来进行修改,对象连接 - 但什么?在的DbContext或数据库?我有点失去了什么这(附加)实际上意味着什么?

Question part 1 I'm trying to get my head around how the dbcontext works - Before I've set the State on the dbContext.Entry(issue), I assume my issue object is detached. Once I set the state to be modified, the object is attached - but to what? the dbContext or the database? I'm kind of missing what this (attaching) actually means?

问题的一部分2
为了便于讨论,让我们说,我决定来设置我的问题的接受字段。接受是一个布尔值。我开始使用它是假的,我在的形式,它设置为true,并提交。在我的对象连接点,什么是OriginalValues​​收集点?例如,如果我设置一个断点只设置EntityState.Modified之后,但在我的SaveChanges调用()我可以查询

Question part 2 For argument's sake, let's say I decide to set the "Accepted" field on my issue. Accepted is a boolean. I start with it being false, I'm setting it to true in the form and submitting. At the point that my object is attached, what is the point of the OriginalValues collection? for example if I set a breakpoint just after setting EntityState.Modified but before I call SaveChanges() I can query

db.Entry(issue).OriginalValues["Accepted"]

和这会给我同样的价值,只是查询已经到编辑已经通过发行对象....即。它被赋予相同的结果

and this will give me the same value as simply querying the issue object that has been passed in to the Edit....i.e. it is giving the same result as

issue.Accepted

我清楚地失去了一些东西,因为文件说
原来的值通常是实体的属性值,因为他们当从数据库中查询最后一个。
但这种情况并非如此,因为数据库仍在报告接受为假(是的,我注意到,在文档的词一般,但我的code是由MS code产生的所有pretty多标准所以....)。
所以,我缺少什么?什么是真正回事?

I'm clearly missing something because the documentation says "The original values are usually the entity's property values as they were when last queried from the database." But this is not the case because the database is still reporting Accepted as being false (yeah, I noted the word "usually" in the docs but my code is all pretty much standard generated by MS code so....). So, what am I missing? what is actually going on here?

推荐答案

上下文只能与连接的实体工作。安装部件这方面了解的实体,它能够坚持它的数据,在某些情况下,它可以提供先进的功能,如更改跟踪或延迟加载。

The context can work only with attached entities. The attaching means that context know about the entity, it can persists its data and in some cases it can provide advanced features like change tracking or lazy loading.

默认情况下从上下文实例数据库中加载所有实体连接到该实例。在Web应用程序和其他断开的情况下如果你对每一个处理HTTP请求新的上下文实例(如果你不知道你正在做一个很大的错误)。还通过HTTP POST模型绑定创建了实体不加载由环境 - 它被分离。如果你想坚持的实体必须重视它,并通知有关的上下文你做了改变。设置状态输入修改会做这两个操作 - 它将附加实体的背景下,并设置其全球状态修改这意味着所有的标量和复杂的属性将当你调用的SaveChanges

By default all entities loaded from the database by the context instance are attached to that instance. In case of web applications and other disconnected scenarios you have a new context instance for every processed HTTP request (if you don't you are doing a big mistake). Also your entity created by model binder in HTTP POST is not loaded by that context - it is detached. If you want to persist that entity you must attach it and inform context about changes you did. Setting state to Entry to Modified will do both operations - it will attach entity to the context and set its global state to Modified which means that all scalar and complex properties will be updated when you call SaveChanges.

因此​​,通过设置状态修改您所附加的实体范围内,但要等到叫的SaveChanges 它会不影响您的数据库。

So by setting state to Modified you have attached the entity to the context but until you call SaveChanges it will not affect your database.

OriginalValues​​ ,你从数据库中加载实体,并进行更改附加实体是完全附着的情况是非常有用。在这种情况下 OriginalValues​​ 显示从数据库加载属性值和 CurrentValues​​ 显示您的应用程序设置的实际值。在您的方案上下文不知道原始值。它认为,当你连接实体原始值的使用。

OriginalValues are mostly useful in fully attached scenarios where you load entity from the database and make changes to that attached entity. In such case OriginalValues show property values loaded from database and CurrentValues show actual values set by your application. In your scenario context doesn't know original values. It thinks that original values are those used when you attached the entity.

这篇关于的DbContext,状态和初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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