我有一个具有以下结构的实体框架POCO .
public class Entity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
我为这个实体创建了一个数据传输对象供我的视图使用 .
public class EntityDto
{
public int Id { get; set; }
public string Name { get; set; }
}
现在,我在Global.asax文件中有以下映射代码 .
Mapper.CreateMap();
Mapper.CreateMap(); // not sure whether I need this as well?
一切正常,我将DTO传递给我的观点确定,我可以从我的 EntityDto 模型中创建 Entity 的新实例 . 当我尝试编辑 Entity 时出现问题;我似乎是一个明确的解决方案 . 这是我用来编辑我的实体的动作 .
public ActionResult EditEntity(EntityDto model)
{
var entity = context.Entities.Single(e => e.Id == model.Id);
entity = Mapper.Map(model); // this loses the Entity Key stuff
context.SaveChanges();
return View(model);
}
现在,我该怎么做才能解决这个问题?我可以吗:
以某种方式告诉AutoMapper .Ignore() 实体密钥属性?
获取AutoMapper以复制实体密钥属性?
.Attach() 我的映射 Entity 并将状态设置为已修改?
任何帮助总是赞赏 .