I am looking for a more better way to update a record using Entity Framework.
Below is what I am using now, where playerFromModel is data fetched from View.
public bool UpdatePlayer(PlayerEntity playerFromModel)
{
bool updateSuccessfull = false;
using (var context = new PlayerEntities())
{
var player = context.Player.Where(m => m.emailAddress == playerFromModel.EmailAddress).FirstOrDefault();
if (player != null)
{
player.emailAddress = playerFromModel.EmailAddress;
player.first_name = playerFromModel.FirstName;
player.last_name = playerFromModel.LastName;
player.alt_email_id1 = playerFromModel.AlternateEmailAddress;
player.street = playerFromModel.Street;
player.city = playerFromModel.City;
player.state = playerFromModel.State;
player.zip = playerFromModel.Zip;
player.country_code = playerFromModel.CountryCode;
player.phone1 = playerFromModel.Phone;
try
{
context.SaveChanges();
updateSuccessfull = true;
}
catch
{
updateSuccessfull = false;
}
}
}
return updateSuccessfull;
}
As you can see I have to manually map each of the fields and the same approach I have followed everywhere.
I was thinking that there definitely has to be a better way to do this.
Please guide me on this. Thanks.
You can just use TryUpdateModel. e.g.
bool updateSuccessfull = false;
using (var context = new PlayerEntities())
{
var player = context.Player.Where(m => m.emailAddress == playerFromModel.EmailAddress).FirstOrDefault();
if (player != null)
{
//Beware this will try to map anything it can. This can be dangerous
if(TryUpdateModel(player)){
try
{
context.SaveChanges();
updateSuccessfull = true;
}
catch
{
updateSuccessfull = false;
}
}
}
}
return updateSuccessfull;
}
I wouldn't though see here Real example of TryUpdateModel, ASP .NET MVC 3.
You could use Automapper or something similar as MiBu said. Create a ViewModel link is here of the Player and then map from that to the entity
//Get a PlayerUpdate ViewModel
using (var context = new PlayerEntities())
{
// fetch the domain model that we want to update - BTW I'd use a repository pattern but that is another debate
var player = context.Player.Where(m => m.emailAddress == playerFromModel.EmailAddress).FirstOrDefault();
if (player != null)
{
// Use AutoMapper to update only the properties of this domain model
// that are also part of the view model and leave the other properties unchanged
AutoMapper.Map<UpdatePlayerViewModel , PlayerEntity>(viewModel, player);
try
{
context.SaveChanges();
updateSuccessfull = true;
}
catch
{
updateSuccessfull = false;
}
}
However I am of the opinion that updating entity properties is a significant thing and shouldn't be done automatically. Others feel the same. Jimmy Bogard who created AutoMapper doesn't seem to believe it needs two way mapping (However he says that is partly as they built AutoMapper for their requirement) Similar answer here on Stackoverflow saying do it yourself.
It depends on the complexity of your application but I would look at using the command pattern to send a message to an appropriate handler containing your properties and have the handler update them. If it was successful we do one thing if it wasn't we do another. Same pattern is described in MVC in Action. See similar (here is the link)[http://www.paulstovell.com/clean-aspnet-mvc-controllers] and here is the second link