Sql – Best way to update in Linq To SQL

linqlinq-to-sqlnetvisual-studio-2008

I have several entity classes that I use for parsing fixed width text files as well as utilizing Linq to SQL. I use these classes to parse data from said text files, and compare to data in the database.

One of these entities has a lot of properties, and I don't want to waste time setting each individual property on the Linq result object.

Is there a way to tell Linq "Here's my object, use this to update the record"? Here's code I'm working on:

if (partialContent.MonthlyAddChange == "A")
   {
       bookContentTable.InsertOnSubmit(partialContent);
   }
   else if (partialContent.MonthlyAddChange == "C")
   {
       var query = from bookContent in bookContentTable
                   where bookContent.EAN == partialContent.EAN
                   select bookContent;

       if (query != null)
       {
           // Do something with query.First()
       }
   }
}

Is it better to delete the record and do an InsertOnSubmit() in this case?

Best Answer

I think the concept of editing a record is different from deleting and inserting a new one. Basically, I think an ORM should abstract away primary key generation and other related stuff. By deleting and inserting, you might be removing the integrity of the record (probably issuing a new primary key, making referenced entities invalid and so forth...). I suggest updating the record whenever the action you are taking is conceptually an update.