views:

257

answers:

2

Hello, I'm new to EF and I've been using the asp.net mvc databind method to update my models "tryupdatemodel", now I have to update a entity from a service layer, since TryUpdateModel is a asp.net mvc method I can't use it in the services layer.

What I need to do to update the data of an entity without using this method?

I'm using repositories.

A: 

There's no automatic way for doing that, you must load the entity from the database and update the properties yourself.

VinTem
What does it have in common with his question?
LukLed
Actually it does have, I'm using helper method to update data without having to do a database call, You just have to attach the entity to the context(With the atta EF method) then you genereta a entity key for the entity and finally mark all the properties as SetModifiedProperty with a foreach method. Then you can save and Voalá, its done. You just call the method pass the entity and the entity set. If anyone has a better approach please tell me ;) Thanks"
Rick
@Rick: Doing it manually is always a possibility, but we are talking about not doing is this way. And there are ways to do it, so this answer is wrong.
LukLed
I'm sorry, if you a better way then show it, cause I dont. And he said that he has to do it outside the controller in a service layer, where I am understanding he doesn't have access to the TryUpdateModel method.
VinTem
A: 

Don't use it in service layer. You could place similar code in Controller:

var entity = Service.Get(id); //get updated entity
TryUpdateModel(entity);
if (ModelState.IsValid)
  Service.Save(entity); //here you can save to database and perform additional validation
LukLed
Well, I can't do in the controlles since I'm using WCF and doing service calls to update data.
Rick
I don't know how exactly your calls are made and my WCF skills are mediocre, so please provide some details.
LukLed