更好地利用更新实体框架的记录方式 [英] Better way to update a record using Entity Framework

查看:147
本文介绍了更好地利用更新实体框架的记录方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个更更好的方式使用实体框架更新记录。

下面是我在用的,现在,这里playerFromModel是查看数据牵强。

 公共BOOL UpdatePlayer(PlayerEntity playerFromModel)
{
    布尔updateSuccessfull = FALSE;    使用(VAR上下文=新PlayerEntities())
    {
        VAR球员= context.Player.Where(M = GT; m.emailAddress == playerFromModel.EmailAddress).FirstOrDefault();        如果(玩家!= 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.Country code;
            player.phone1 = playerFromModel.Phone;            尝试
            {
                context.SaveChanges();
                updateSuccessfull = TRUE;
            }
            抓住
            {
                updateSuccessfull = FALSE;
            }
        }
    }    返回updateSuccessfull;
}

正如你可以看到我有手动映射每个我都遵循到处领域和相同的方法。

我在想,这里绝对必须有一个更好的方式来做到这一点。

请指导我在此。谢谢你。


解决方案

您可以只使用<一个href=\"http://stackoverflow.com/questions/5268421/when-and-why-do-you-use-tryupdatemodel-in-asp-net-mvc-2\">TryUpdateModel.例如。

 布尔updateSuccessfull = FALSE;
    使用(VAR上下文=新PlayerEntities())
    {
        VAR球员= context.Player.Where(M = GT; m.emailAddress == playerFromModel.EmailAddress).FirstOrDefault();        如果(玩家!= NULL)
        {
             //当心,这将尝试映射任何东西就可以了。这可能是危险的
            如果(TryUpdateModel(播放器)){
            尝试
            {
                context.SaveChanges();
                updateSuccessfull = TRUE;
            }
            抓住
            {
                updateSuccessfull = FALSE;
            }
         }
        }
    }
    返回updateSuccessfull;
}

我不看,虽然TryUpdateModel,ASP这里真实的例子.NET MVC 3

您可以使用Automapper或靡不说过类似的话。创建一个视图模型链接这里的播放机,然后从映射到该实体

  //获取PlayerUpdate视图模型       使用(VAR上下文=新PlayerEntities())
    {
   //获取我们想要更新的领域模型 - 顺便说一句我会使用一个存储库模式,但那是另一辩论
        VAR球员= context.Player.Where(M = GT; m.emailAddress == playerFromModel.EmailAddress).FirstOrDefault();        如果(玩家!= NULL)
        {        //使用AutoMapper更新此域模型只有属性
        //同时也是视图模型的一部分​​,并保留其他属性不变
        AutoMapper.Map&LT; UpdatePlayerViewModel,PlayerEntity&GT;(视图模型,播放器);           尝试
            {
                context.SaveChanges();
                updateSuccessfull = TRUE;
            }
            抓住
            {
                updateSuccessfull = FALSE;
            }
    }

不过,我认为更新实体属性是显著的事情,不应该自动完成的。其他人有同样的感觉。吉米·博加德谁创造AutoMapper似乎不相信它需要的双向映射(不过他说,这是部分原因是他们建立AutoMapper他们的要求)类似的答案在这里<一个href=\"http://stackoverflow.com/questions/7588907/asp-net-mvc-should-i-use-automapper-from-viewmodel-to-entity-framework-entitie\">on计算器的说自己做。

这取决于应用程序的复杂性,但我想看看使用命令模式将消息发送到包含性能合适的处理器,并有处理程序更新。如果它是成功的,我们做一件事,如果不是我们做一套。同样的模式在描述rel=\"nofollow\"> MVC。看到类似的(这里是链接)[http://www.paulstovell.com/clean-aspnet-mvc-controllers]这里的

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

这篇关于更好地利用更新实体框架的记录方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆