映射一个DTO与Automapper实体 [英] Mapping a DTO to an Entity with Automapper

查看:101
本文介绍了映射一个DTO与Automapper实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体框架POCO具有下列结构。

 公共类实体
{
公共虚拟INT标识{搞定;组; }
公共虚拟字符串名称{;组; }
}



我创建了一个数据传输对象由使用该实体。我的看法

 公共类EntityDto 
{
公众诠释标识{搞定;组; }
公共字符串名称{;组; }
}

现在,我在Global.asax文件下面的映射代码。

  Mapper.CreateMap<实体,EntityDto>(); 
Mapper.CreateMap< EntityDto,实体>(); //不知道我是否需要这个呢?



一切工作正常,我通过DTO我的意见确定,我可以创造一个新的实例实体从我 EntityDto 模式。当我试图编辑我的实体问题出现;我知道这是下降到AutoMapper失去了EF创建跟踪更改对象的实体按键,而是通过几个来源看了似乎没有成为一个明确的解决办法。下面是我用编辑我的实体的动作。

 公众的ActionResult EditEntity(EntityDto模型)
$ { b $ b VAR实体= context.Entities.Single(E => e.Id == model.Id)​​;
实体= Mapper.Map< EntityDto,实体>(模型); //这个失去了实体按键的东西
context.SaveChanges();

返回查看(模型);
}

现在,我该怎么办,以解决此问题?我可以:




  1. 不知怎的告诉AutoMapper到结尾。可以忽略()实体键的属性?

  2. 获取AutoMapper复制出来的实体键的属性?

  3. .Attach()我的映射实体键,状态设置为修改?



任何帮助总是赞赏


解决方案

.Attach()我的映射实体状态设置为修改?




 公众的ActionResult EditEntity(EntityDto模型)
{
VAR实体=映射。地图<实体GT;(模型);
context.Set<实体方式>()将(实体); //(或context.Entity.Attach(实体);)
context.Entry<实体GT;(实体).STATE = System.Data.EntityState.Modified;
context.SaveChanges();
返回查看(模型);
}



哪里是您的上下文实例化?你应该这样做,在你的EditEntity行动海事组织。

 公众的ActionResult EditEntity(EntityDto模型)
{
使用(VAR上下文=新MyContext())
{
VAR实体= Mapper.Map<实体GT;(模型);
context.Set<实体方式>()将(实体); //(或context.Entity.Attach(实体);)
context.Entry<实体GT;(实体).STATE = System.Data.EntityState.Modified;
context.SaveChanges();
返回查看(模型);
}
}


I have an Entity Framework POCO with the following structure.

public class Entity
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

I've created a Data Transfer Object for this entity to be used by my views.

public class EntityDto
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Now, I have the following mapping code in my Global.asax file.

Mapper.CreateMap<Entity, EntityDto>();
Mapper.CreateMap<EntityDto, Entity>(); // not sure whether I need this as well?

Everything is working fine, I pass the DTO to my views OK and I can create a new instance of Entity from my EntityDto model. The problem arises when I try to edit my Entity; I'm aware this is down to AutoMapper losing the Entity Key that EF creates to track changes to the object, but having read through a few sources there doesn't seem to be a definitive solution. Here is the action I'm using to edit my entity.

public ActionResult EditEntity(EntityDto model)
{
    var entity = context.Entities.Single(e => e.Id == model.Id);
    entity = Mapper.Map<EntityDto, Entity>(model); // this loses the Entity Key stuff
    context.SaveChanges();

    return View(model);
}

Now, what do I do to solve this? Can I:

  1. Somehow tell AutoMapper to .Ignore() the Entity Key properties?
  2. Get AutoMapper to copy out the Entity Key properties?
  3. .Attach() my mapped Entity and set the state to modified?

Any help always appreciated.

解决方案

.Attach() my mapped Entity and set the state to modified?

public ActionResult EditEntity(EntityDto model)
{
    var entity = Mapper.Map<Entity>(model);
    context.Set<Entity>().Attach(entity); // (or context.Entity.Attach(entity);)
    context.Entry<Entity>(entity).State = System.Data.EntityState.Modified;
    context.SaveChanges();
    return View(model);
}

Where is your context instanciated? You should do that in your EditEntity action imo.

public ActionResult EditEntity(EntityDto model)
{
    using(var context = new MyContext())
    {
        var entity = Mapper.Map<Entity>(model);
        context.Set<Entity>().Attach(entity); // (or context.Entity.Attach(entity);)
        context.Entry<Entity>(entity).State = System.Data.EntityState.Modified;
        context.SaveChanges();
        return View(model);
    }
}

这篇关于映射一个DTO与Automapper实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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