Best Upgrade Method in Linq To SQL

I have several entity classes that I use to parse fixed-width text files, and also to use Linq to SQL. I use these classes to analyze data from specified text files and compare with data in a database.

One of these objects has many properties, and I do not want to waste time setting up each individual property in the Linq result object.

Is there a way to tell Linq "Here is my object, use this to update the record"? Here is the 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 entry and do InsertOnSubmit () in this case?

+6
linq visual-studio-2008 linq-to-sql
source share
5 answers

I think the concept of editing a record is different from deleting and inserting a new one. Basically, I believe that ORM should abstract primary key generation and other related things. By deleting and inserting, you can remove the integrity of the record (it may produce a new primary key, invalidating object references, etc.). I suggest updating the record whenever the action you take is conceptually an update.

+2
source share

I think you can use something like DataContext.Table.Attach (record, true) and then DataContext.SubmitChanges (). But I do not have it completely hidden ...

So now I have done the test. This will only work if you do not require a concurrency check (i.e. you are only updating the table).

Here is my table

People PersonID int FirstName varchar(50) LastName varchar(50) 

I filled the table with the following entry

 > PersonID FirstName LastName > 1 Jason Punyon 

I created a LINQ2SQL DataContext with only this PeopleDataContext table and for each property of the People class I set the UpdateCheck property for each property of the Never record.

Here is the code:

 static void Main(string[] args) { var p = new People(); p.PersonID = 1; p.FirstName = "Jason"; p.LastName = "This is a new last name"; using (var db = new PeopleDataContext()) { db.Peoples.Attach(p, true); db.SubmitChanges(); } } 

And it works successfully. No reflection or anything else, but as I said, you lose the concurrency check.

+1
source share

You can use automapper to copy the values ​​for you. Check this out for more information: http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/01/22/automapper-the-object-object-mapper.aspx

+1
source share

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

No, definitely not - just consider the referential integrity that any good, stable database design should use. If your record is already in use by other lines, you cannot just delete it and insert it again - you will violate these integrity restrictions.

If you just change multiple values, update the existing line - much simpler and much more consistent.

Mark

+1
source share

I would not delete / recreate. Assuming the two objects have the same interface with respect to the properties you want to update, I would use reflection and copy the change values ​​of the existing object. If any of the values ​​differs from the original, the record will be marked as subject to updating, and SubmitChanges will take care of this.

For example (with a little error checking):

  foreach (var bookInfo in bookContent.GetType().GetProperties()) { var partialInfo = partialContent.GetType().GetProperty( bookInfo.Name ); if (partialInfo != null) { bookInfo.SetValue( partialInfo.GetValue( partialContent, null ) ); } } 

If you knew they were of the same type, you can reuse the first PropertyInfo instead of getting a new one for partialContent.

0
source share

All Articles