实体框架,AutoMapper,处理实体更新 [英] Entity Framework, AutoMapper, handling entity updates

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

问题描述

我刚刚开始使用Entity Framework 1.0,并且相信我开始感到每个人都在讨论的痛苦。我试图使用最佳做法,所以我有一套DTO,通过AutoMapper映射到我的实体。



真正的抓住是我正在尝试更新对象。第一个问题是,我找不到创建一个新实体的方法,从我的DTO传输数据,并且仍然有实体ObjectContext意识到它已经被改变。我使用以下代码:

  public VideoDTO UpdateVideo(VideoDTO pVideo)
{
视频视频=新视频();
Mapper.Map(pVideo,video);
context.Attach(video); //成功地附加
context.ApplyPropertyChanges(视频,视频); //没有更改,只要实体知道b / c它被附加在更新状态
context.SaveChanges(); //不保存实体
return pVideo;
}

然后我想,也许我需要从数据库中抓取实体,附加到上下文,在Mapper上调用Map方法,然后调用SaveChanges。在这里我做了什么:

  public VideoDTO UpdateVideo(VideoDTO pVideo)
{
视频视频=上下文。 Videos.Where(v => v.VideoId == pVideo.VideoId).FirstOrDefault();
Mapper.Map(pVideo,video); // Error here:Can not change VideoId value on Video entity
//context.Attach(video);
//context.ApplyPropertyChanges(\"Videos,video);
context.SaveChanges();

return pVideo;
}

现在我们得到了不允许更改财产的可爱的EF问题,VideoId,因为它被视频实体上的EntityKey属性使用。可爱。我设置了映射,所以当我从DTO映射到EF实体时,EntityKey属性将获得一个值。现在我需要一种方法来对映射规则做一个例外,但是不知道从哪里开始。我想我可以在这个方法中创建一个全新的Mapping规则,并设置EntityKey& VideoId属性要被忽略,但是看起来很马虎。此外,我不知道在这一点上创建的映射是否会粘。如果它覆盖初始设置,允许DTO将值映射到实体上的EntityKey,那么将以完全不同的方式反馈。



任何人都有一个更好的想法?

解决方案

AutoMapper



第一个问题是,据我所知,AutoMapper不是设计为从DTO-> Entity only Entity-> DTO。这可能最近有所改变,所以我不太确定。请参阅此链接以获取有关自动化机床设计的更多信息:双向映射的情况



PK映射



你说:在这个方法中映射规则,并设置EntityKey& VideoId属性被忽略,但是这似乎很草率



我根本不认为这是疯狂的。实体坚持不用触摸EntityKey / PK,可能应该以某种方式编辑它的静态。



实体框架 p>

现在我们看到可爱的EF问题,不允许更改属性VideoId,因为它被视频实体上的EntityKey属性使用可爱。



可爱吗? EF不会强迫您不更新您的PK。在生成的模型中,您的键的设置器内有一个属性更改检查。解决方案是更改生成的代码。根据您的模型波动性,这可能不实用,但它是一种选择。


I just started using the Entity Framework 1.0 recently and believe I am beginning to feel the pains everyone is talking about. I'm trying to use best practices so I have a set of DTO that get mapped to and from my Entities via AutoMapper.

The real catch is when I'm trying to update an object. The first gotcha was that I could not find a way to create a new entity, transfer the data from my DTO, and still have the entity ObjectContext realize that it has been changed. I used the following code:

public VideoDTO UpdateVideo(VideoDTO pVideo)
        {
            Video video = new Video();
            Mapper.Map(pVideo, video);
            context.Attach(video); //Successfully attaches
            context.ApplyPropertyChanges("Videos", video);  // no changes made as far as entity knows b/c it was attached in it's updated state
            context.SaveChanges(); //doesn't save the entity                
            return pVideo;
        }

I then figured, perhaps I need to just grab the entity from the database first, attach to the context, call the Map method on Mapper, then call SaveChanges. Here what I did:

    public VideoDTO UpdateVideo(VideoDTO pVideo)
    {
        Video video = context.Videos.Where(v => v.VideoId == pVideo.VideoId).FirstOrDefault();
        Mapper.Map(pVideo, video); //Error here: Can't change VideoId value on Video entity
        //context.Attach(video);
        //context.ApplyPropertyChanges("Videos", video);
        context.SaveChanges();

        return pVideo;
    }

Now we get to the lovely EF issue of not being allowed to change the property, VideoId, because it's used by the EntityKey property on the Video entity. Lovely. I had setup the mappings so that when I mapped from my DTO to an EF Entity, the EntityKey property would get a value. Now I need a way to make an exception to that mapping rule, but have no clue where to begin. I suppose I could create a brand new Mapping rule right in this method and set the EntityKey & VideoId properties to be ignored, but that seems pretty sloppy. Furthermore, I'm not sure a mapping created at this point would stick. If it overrode the initial setup that allowed the DTO to map a value to the EntityKey on the entity, that would backfire in a whole different way.

Anyone have a better idea?

解决方案

AutoMapper

Your first problem is that as far as I know AutoMapper is not designed to go from DTO->Entity only Entity->DTO. This could have changed recently so I'm not really sure. See this link for more information about what automapper is designed to do: The case for two way mapping

PK Mapping

You say: "Mapping rule right in this method and set the EntityKey & VideoId properties to be ignored, but that seems pretty sloppy"

I don't think thats sloppy at all. You really shouldn't touch a EntityKey/PK after its been persisted and probably should codify its staticness in some way.

Entity Framework

"Now we get to the lovely EF issue of not being allowed to change the property, VideoId, because it's used by the EntityKey property on the Video entity. Lovely."

Lovely? EF is not forcing you to not update your PK. Inside the generated models there is a property change check inside the setter for your keys. The solution would be to change the generated code. Depending on your model volatility this may not be practical but it is an option.

这篇关于实体框架,AutoMapper,处理实体更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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