0

What is the best practice to insert differential records into the database using EF 4.0

I have a collection of Apartments in an object with 3 records.

Before inserting, I retrieve records from the database (Apartments Table) and brings back only 1 record. That 1 records happens to be 1 out of the 3 records in the objects above. I dont want to insert duplicate records.

I can remove the one different record and insert all 3 again...

Is there a better way of doing it? What is the best way of inserting only the 2 objects that are different to that table using EF?

Mage
  • 969
  • 2
  • 13
  • 23
  • Just insert only the two that are different? Since you load what is already in the DB you know what is different, don't you? I don't understand where the problem is... – Slauma Nov 10 '12 at 18:56
  • Use Except() to produce the set difference of your two collections. http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except.aspx – Matija Grcic Nov 10 '12 at 20:34
  • Is there anyway to do it without querying the db? – Mage Nov 10 '12 at 20:35
  • Try looking at http://stackoverflow.com/a/5558777/1241400 – Matija Grcic Nov 10 '12 at 20:41

0 Answers0