简体   繁体   中英

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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