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

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

问题描述

我刚开始使用实体框架1.0和最近相信我开始感觉到大家都在谈论的痛苦。我试图让我有一组DTO的是被映射到从我的实体通过AutoMapper使用的最佳实践。

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.

真正美中不足的是,当我试图更新的对象。第一个疑难杂症的是,我无法找到一种方法来创建一个新的实体,从我的DTO传输数据,而且还有ObjectContext的意识到,它已经改变了实体。我用下面的code:

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;
        }

我后来想通,也许我只是需要抓住从数据库实体第一,重视的背景下,调用地图的方法上映射,然后调用调用SaveChanges。在这里,我所做的:

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;
    }

现在我们得到的不是被允许更改属性,VIDEOID可爱的EF问题,因为它使用的视频实体的EntityKey属性。可爱。我不得不设置的映射,这样,当我从我的DTO到EF实体映射,该物业的EntityKey会得到一个值。现在我需要一种方法,使一个例外,即映射规则,但不知道从哪里开始。我想我可以就在此方法中创建一个全新的映射规则和设置的EntityKey&放大器; VIDEOID属性被忽略,但似乎pretty马虎。此外,我不知道在这一点上创建一个映射会坚持。如果推翻了允许DTO对实体映射一个值的EntityKey初始设置,这将适得其反在一个完全不同的方式。

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

您的第一个问题是,据我知道AutoMapper的目的不是从DTO->实体只有实体 - > DTO去。这可能最近更改,所以我真的不知道。请参阅此链接什么automapper的目的是做的更多信息:<一href=\"http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/09/17/the-case-for-two-way-mapping-in-automapper.aspx\">The案例双向映射

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映射

您说:这个方法映射规则的权利,设置的EntityKey&放大器; VIDEOID属性被忽略,但似乎pretty马虎

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

我不认为这就是马虎的。你真的不应该碰的EntityKey / PK对其进行了持续后可能应该编纂其staticness以某种方式。

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.

实体框架

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

"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."

可爱? EF不会强迫你不更新你的PK。在内部生成的模型有二传手为您锁中的属性更改检查。该解决方案将是改变产生code。根据型号的波动,这可能是不实际的,但它是一种选择。

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天全站免登陆