为什么要使用GetOriginalEntityState()在我的LINQ to SQL资料库保存方法? [英] Why should I use GetOriginalEntityState() in my LINQ To SQL repository save method?

查看:118
本文介绍了为什么要使用GetOriginalEntityState()在我的LINQ to SQL资料库保存方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待在从史蒂芬·桑德森的书产品资源库保存方法的例子,临ASP.NET MVC 2框架:

I'm looking at an example of a save method in a Products repository from Steven Sanderson's book, Pro ASP.NET MVC 2 Framework:

public void SaveProduct(Product product)
{
    // if new product, attach to DataContext:
    if (product.ProductID == 0)
        productsTable.InsertOnSubmit(product);
    else if (productsTable.GetOriginalEntityState(product) == null)
    { 
        // we're updating existing product
        productsTable.Attach(product);
        productsTable.Context.Refresh(RefreshMode.KeepCurrentValues, product);
    }
    productsTable.Context.SubmitChanges();            
}

我不明白的逻辑否则,如果行:

else if (productsTable.GetOriginalEntityState(product) == null)

据我了解, GetOriginalEntityState()返回在这种情况下该实体是产品指定实体的原始状态..

As I understand it, GetOriginalEntityState() returns the original state of the specified entity.. in this case that entity is product.

所以这个else if语句可读取我喜欢的:如果一个原本并不存在,那么......的,而是因为这本书是说这个检查,我们这没有意义正在修改已经确实存在的记录。

So this else if statement reads to me like: "If an original doesn't exist then..." but that doesn't make sense because the book is saying that this checks that we are modifying a record that already DOES exist.

我应该如何理解 GetOriginalEntityState 在这种情况下?

How should I understand GetOriginalEntityState in this context?

顺便说一句,这个摘录来自第6章,191页......以防万一任何人有这本书,想看看它。这本书只是有code样品中的功能,但它从来没有解释函数做什么。

By the way, this excerpt came from chapter 6, page 191... just in case anyone has the book and wants to look it up. The book just has that function in the code sample but it never explains what the function does.

推荐答案

这是一个猜测一点点,因为我从来没有实际使用 GetOriginalEntityState 但问题见顶我兴趣搞清楚是怎么回事。

This is a little bit of a guess since I have never actually used GetOriginalEntityState but the question peaked my interest to figure out what is going on.

我觉得这里的目的是检查产品仍连接到原始的DataContext

I think the intent here is to check that product is still attached to the original DataContext

行:

if (productsTable.GetOriginalEntityState(product) == null) 

我想,如果产品已独栋或创建由的DataContext 手动和没有处理这将返回null。

I think this will return null if product has been dettached or created manually and not handled by the DataContext.

此方法将返回原来的状态
  一个实体的,因为这是不是
  创建或附加到当前
  DataContext的。的原始状态
  已序列化的实体,
  反序列化必须由提供
  独立的跟踪机制和
  当实体附接供给
  到一个新的DataContext。欲了解更多
  信息,请参阅数据检索和
  CUD操作的N层应用程序
  (LINQ到SQL)。

This method returns the original state of an entity since it was either created or attached to the current DataContext. The original state of an entity that has been serialized and deserialized must be provided by an independent tracking mechanism and supplied when the entity is attached to a new DataContext. For more information, see Data Retrieval and CUD Operations in N-Tier Applications (LINQ to SQL).

我觉得关键线路明白的是:

I think the key line to understand is:

此方法将返回原来的状态
  一个实体的,因为这是不是
  创建或附加到当前
  DataContext的。

This method returns the original state of an entity since it was either created or attached to the current DataContext.

GetOriginalEntityState 用于使该方法能收到不是已经连接到的DataContext 。附加的意义,通过一个LINQ VS只是创造像产品P =新产品(一个实例返回SQL调用){...}; 。如果还没有安装,将其附加到的DataContext 并保存任何被修改的值​​(preserving更新的值),由于 RefreshMode.KeepCurrentValues​​ 参数。

GetOriginalEntityState is used so that the method can receive an object with the option of not being attached already to the DataContext. Attached meaning, returned by a Linq To SQL call vs just creating an instance like Product p = new Product() { ... };. If it is not attached, it will attach it to the DataContext and keep any of the values that were modified (preserving the update values) due to the RefreshMode.KeepCurrentValues param.

然后 productsTable.Context.SubmitChanges(); 总是因为即使是独栋发生时, GetOriginalEntityState 将确保它被附加这样的提交将正常工作。

Then the productsTable.Context.SubmitChanges(); always happens since even if it is dettached, the GetOriginalEntityState will make sure it gets attached so the submit will work.

这篇关于为什么要使用GetOriginalEntityState()在我的LINQ to SQL资料库保存方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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