I am trying to update a POCO using lin2sql. I can also use entity framework. For updating objects I follow the next routine.
//GridView Control gives me some updated POCOS As an Example: Person updated;
function UpdatePerson(Person myUpdatedPersonfromUI)
{
using (Entity con = new Entity() ) {
var recordFromdB = from obj in con.Person where obj.PK = myUpdatedPersonfromUI.PK
select obj;
Person personOnDB = recordFromdB.Single();
// now for each column I update personOnDB
personOnDB.Property1 = myUpdatedPersonfromUI.Property1 ;
personOnDB.Property2 = myUpdatedPersonfromUI.Property2 ;
personOnDB.Property3 = myUpdatedPersonfromUI.Property3 ;
personOnDB.Property4 = myUpdatedPersonfromUI.Property4 ;
// continue updating fields ...
..
.
personOnDB.Property124 = myUpdatedPersonfromUI.Property124 ;
con.SaveChanges();
}
}
Do I have to update each property manually . Please help .
You can use an object mapping tool like AutoMapper which pretty much will do the work for you – in this simple case (property names match between source and target) it would be a one-liner to map these.