1

I am developing an HRM application to import and export xml data from database. The application receives exported xml data for the employee entry. I imported the xml file using linq to xml, where I converted the xml into respective objects. Then I want to attach (update) the employee objects.

I tried to use

//linqoper class for importing xml data and converts into IEnumerable employees object.
  var emp = linqoper.importxml(filename.xml);
  Using (EmployeedataContext  db = new EmployeedatContext){
  db.attachAllonSubmit(emp);
  db.submitchange();
 }

But I got error “An entity can only be attached as modified without original state if it declares as version member or doesn't have an update check policy”.

I have also an option to retrieve each employee, and assign value to the new employee from xml data using this format.

//import IEnumerable of Employee objects
var employees = = linqoper.importxml(filename.xml)
using(Employeedatacontext db = new Employeedatacontext){
foreach(var empobj  in employees)
{
 Employee emp = db.Employee.where(m=>m.id==empobj.Id);
emp.FirstName=empobj.FirstName;
emp.BirthDate=empobj.BirthDate;
//….continue 
}
db.submitChanges();
}

But the problem with the above is I have to iterate through the whole employee objects, which is very tiresome. So is there any other way, I could attach (update) the employee entity in the database using LINQ to SQL.

I have seen some similar links on SO, but none of them seems to help.

https://stackoverflow.com/questions/898267/linq-to-sql-attach-refresh-entity-object

Community
  • 1
  • 1
xshaw
  • 311
  • 2
  • 12

2 Answers2

1

When linq-to-sql saves the changes to the database, it has to know properties of the object has been changed. It also checks if a potentially conflicting update to the database have been done during the update (optimistic concurrency).

To handle those cases LINQ-to-SQL needs two copies of the object when attaching. One with the original values (as present in the DB) and one with the new, changed values. There is also a more advanced mechanism involving a version member which is mapped to a rowversion column.

The linq-to-sql way to update a set of data is to first read all data from the database, then update the objects retrieved form the database and finally call SubmitChanges(). That would be my first approach in your situation.

If you experience performance problems, then it's time to go outside of linq-to-sql's toolbox. A solution with better performance is to load the new data into a separate staging table (for best performance, use bulk insert). Then run a SQL command or Stored Procedure that does the actual merging of data. The SQL Merge clause is excellent for this kind of updates.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • Thank you @Anders-Abel, So do you mean there is no way to attach a collection of object ripped from XML file using the attach method in LINQ to SQL. – xshaw Mar 07 '12 at 08:02
0

LINQ to SQL is proper ORM, but if you want to take control of create/update/delete in your hand; than you can try some simple ORMs which just provide ways to do CRUD operations. I can recommend one http://crystalmapper.codeplex.com, it is simple yet powerful.

Why CrystalMapper?

I built this for large financial transaction system with lots of insert and update operations. What I need is speed and control of insert/update serving complex business scenarios ... hitting multiple tables just for one transaction.

When I put this to use in social text processing platform, it serves very well there too.

Faraz M. Khan
  • 159
  • 1
  • 4