I have an Entity Framework POCO framework with the following structure.
public class Entity { public virtual int Id { get; set; } public virtual string Name { get; set; } }
I created a data transfer object for this object that will be used by my views.
public class EntityDto { public int Id { get; set; } public string Name { get; set; } }
Now I have the following conversion code in my Global.asax file.
Mapper.CreateMap<Entity, EntityDto>(); Mapper.CreateMap<EntityDto, Entity>(); // not sure whether I need this as well?
Everything works fine, I pass the DTO to my OK views, and I can create a new Entity instance from my EntityDto model. The problem occurs when I try to change my Entity ; I know this is before AutoMapper, which lost the Entity key that EF creates to track object changes, but after reading a few sources, it does not seem to be the final solution. Here is the action I use 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);
Now, what should I do to solve this problem? Can I:
- How to say AutoMapper for
.Ignore() properties of an Entity Key object? - Get AutoMapper to copy Entity Key object properties?
.Attach() my associated Entity and set the state to change?
Any help is always appreciated.
Paul aldred-bann
source share