简体   繁体   English

是否可以使用linq to sql从数据库制作同一记录的两个副本?

[英]is it possible to make two copies of the same record from a db using linq to sql?

I want to have two copies of the same linq to sql object/DB record. 我想拥有同一linq到SQL对象/数据库记录的两个副本。 Is this possible? 这可能吗? I tried to do this by making two records from DB (via the code below) but this seems to just create aliases for the same underlying db record. 我试图通过从数据库中创建两条记录(通过下面的代码)来做到这一点,但这似乎只是为同一基础数据库记录创建别名。 Is it possible to make two distinct copies of the same record? 是否可以为同一记录制作两个不同的副本? I am trying to get two references pointing to two different objects. 我试图得到两个指向两个不同对象的引用。 (If this is not possible, I guess I can just clone the object) (如果这不可能,我想我可以克隆该对象)

    Dim one As Rec= (From l In myDB.table Where l.pk = 222 Select l).First
    Debug.Print(one.ssn) //prints 666666666  
    Dim two As Rec = (From l In myDB.table Where l.pk = 222 Select l).First
    two.ssn= "555555555"  //this also changes one.ssn to "555555555", but I don't want this to happen

The most straight-forward way would be to clone the object. 最简单的方法是克隆对象。 That would most clearly express your intent. 那将最清楚地表达您的意图。 The only other way (since Linq to SQL does not have a detach method) would be to create an entirely new DataContext and call InsertOnSubmit() with your object. 唯一的其他方法(由于Linq to SQL没有分离方法)是创建一个全新的DataContext并用您的对象调用InsertOnSubmit()。 This should work assuming your primary key is generated, otherwise you'll have to set a new primary key yourself. 假设您的主键已生成,这应该可以工作,否则您必须自己设置一个新的主键。 But that seems like something of a hack - I like the first solution better. 但这似乎有点不合时宜-我更喜欢第一个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM