什么是LINQ到SQL表< T> .Attach呢? [英] What does LINQ-to-SQL Table<T>.Attach do?

查看:133
本文介绍了什么是LINQ到SQL表< T> .Attach呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

究竟做了LINQ到SQL方法表< T> .Attach()表< T> .AttachAll(),什么是对他们正确使用的例子/局面

What exactly does the LINQ-to-SQL method Table<T>.Attach() and Table<T>.AttachAll() and what is an example/situation for their proper usage?

另外,请查看此相关的问题:?的How从DataContext的分离一个LINQ到SQL数据对象?

Also, please check out this related question: How to detach a LINQ-to-SQL data object from the DataContext?

推荐答案

正是在这种序列化/反序列化数据到其他层的多层应用程序非常有用的。

It is really useful in multi-tier applications that serialize/deserialize data to other layers.

附加()讲述的的DataContext 实体是不是新的(对于插入),但是这意味着一个更新的实体为更新在数据库中。

Attach() tells DataContext the entity is not new (for insert) but an updated entity that is meant to be updated in the DB.

您有的DataContext 在您的实体存在。新的实体被插入,现有的得到更新。现在,你需要一些实体发送到另一个层次,的DataContext ,然后分离所述实体,并将其发送了。结果
在其他层的实体被修改,并发送回您的数据层。现在,前者的的DataContext 那有你的实体可能不再存在(例如,如果是无状态的),或者不知道你反序列化的实体,那么你会怎么做?您创建一个新的的DataContext 或使用现有的,并使用附加()法 - 这种方式在的DataContext 知道实体,就是要的更新并不宜插入到数据库中。

You have a DataContext where your entities exist. New entities get inserted, existing ones get updated. Now you need to send some entity to another tier, DataContext then detaches said entity and sends it away.
On the other tier the entity gets modified and sent back to your data layer. Now the former DataContext that had your entity may not exist anymore (eg. if it is stateless) or does not know your deserialized entity so what do you do? You create a new DataContext or use the existing one and use the Attach() method - this way the DataContext knows the entity is meant to be updated and should not be inserted into the database.

这同样适用于 AttachAll()但对于多个实体。

The same goes for AttachAll() but for multiple entities.

这篇关于什么是LINQ到SQL表&LT; T&GT; .Attach呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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