开发者

linq2sql best way to update POCO having many columns

开发者 https://www.devze.com 2023-04-04 09:28 出处:网络
I am trying to update a POCO using lin2sql. I can also use entity framework.For updating objects I follow the next routine.

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 ;  


开发者_如何学Go             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.


Given that you are using you same object type in the UI, why not just plug it into the DAL and call update directly?

This link gives details on how to do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号