[英]Moving objects between lists AutoMapper and EF6
我已经开始使用这个扩展了,只想说它的优秀,谢谢!
现在我有一个问题,一个对象可以从一个集合移动到另一个集合,当我这样做时,我得到一个例外
InvalidOperationException:违反了多重性约束
我猜这是因为该对象没有在原始集合中找到,并且此扩展将对象添加到新集合中,即使我希望它也被移动,然后在保存时,EF抛出异常,因为我有2对我的上下文使用相同键的对象。
但是我怎么能让它发挥作用呢?
所以如果我有以下对象结构
MyRoot
| Collection
| MyChild
| Collection
| MyObject (1)
| MyChild
| Collection
| MyObject (2)
如何将MyObject (1)
移动到与MyObject (2)
相同的集合中?
这些都是基本对象,这里有一些简单的代码
public class MyRoot
{
public int Id { get; set; }
public ICollection<MyChild> MyChildren { get; set; }
}
public class MyChild
{
public int Id { get; set; }
public int RootId { get; set; }
public MyRoot Root { get; set; }
public ICollection<MyObject> MyObjects { get; set; }
}
public class MyObject
{
public int Id { get; set; }
public string Name { get; set; }
public int ChildId { get; set; }
public MyChild Child { get; set; }
}
这些对象中的每一个都有一个DTO,为了这个例子,我们只是说对象完全相同,最后有扩展DTO(在实际应用中不是这种情况)
在我的应用程序中,我有一个自动化配置文件,就像这样
internal class MyProfile: Profile
{
public MyProfile()
{
this.CreateMap<MyRoot, MyRootDTO>()
.ReverseMap();
this.CreateMap<MyChild, MyChildDTO>()
.ReverseMap()
.EqualityComparison((s, d) => s.Id == d.Id);
this.CreateMap<MyObject, MyObjectDTO>()
.ReverseMap()
.EqualityComparison((s, d) => s.Id == d.Id);
}
}
在我的web api控制器方法上,我有这个,这很简单
public async Task<IActionResult> UpdateAsync([FromBody] MyRootDTO model)
{
// get the object and all children, using EF6
var entity = await _service.GetAsync(model.Id);
// map
_mapper.Map(model, entity);
// pass object now updated with DTO changes to save
await _service.UpdateAsync(entity);
// return
return new OkObjectResult(_mapper.Map<MyRootDTO>(entity));
}
如果有人可以请求帮助,那就太好了!
我不认为你的问题与AutoMapper有任何关系,这是一个实体框架问题。 如果从EF中的子集合中删除某些内容,除非您在其上调用.Delete,或者该对象的键是包含父项的复合键,否则它不会自动删除。
我建议制作一个复合键,如下所示:
public class MyObject
{
[Column(Order = 1)]
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
[Column(Order = 0)]
[Key]
public int ChildId { get; set; }
public MyChild Child { get; set; }
}
[DatabaseGenerated]
选项将Id
列保留为Identity - EF的默认值与复合键不具有自动标识。
您可以在MyChild
实体上执行相同的MyChild
。
为了使其工作,我没有更改EF键,但在我的AutoMapper配置文件中实现了一个方法。 我遍历该对象以查看该子项是否在不同的列表中,如果是,则将该对象移动到该新列表中。 这样,automapper仍然能够基于ID匹配对象。
我将以下代码添加到.BeforeMap
方法中
在本例中,我的基本级别对象不是名为Root
,因此参数s
的类型为RootModel
(来自我的web api),参数d
的类型为Root
(来自EF)。 RootModel
和Root
都有一个名为Sections
的集合
.BeforeMap((s, d) =>
{
// we are going to check if any child has been moved from 1 parent to another, and
// if so, move the child before the mapping takes place, this way AutoMapper.Collections will not
// mark the object as orphaned in the first place!
foreach (var srcParent in s.Sections)
{
// only loop through old measures, so ID will not be zero
foreach (var srcChild in srcParent.Children.Where(e => e.Id != 0))
{
// check if the srcChild is in the same dest parent?
var destChild = d.Sections.SelectMany(e => e.Children).Where(e => e.Id == srcChild.Id).FirstOrDefault();
// make sure destination measure exists
if (destChild != null)
{
// does the destination child section id match the source section id? If not, child has been moved
if (destChild.ParentId != srcParent.Id)
{
// now we need to move the child into the new parent, so lets find the destination
// parent that the child should be moved into
var oldParent = destChild.Parent;
var newParent = d.Sections.Where(e => e.Id == srcParent.Id).FirstOrDefault();
// remove child from children collection on oldSection and add to newSection
oldParent.Children.Remove(destChild);
// if newParent is NULL, it is because this is a NEW section, so we need to add this new section
// NOTE: Root is my based level object, so your will be different
if (newParent == null)
{
newParent = new Parent();
d.Sections.Add(newParent);
newParent.Root = d;
newParent.RootId = d.Id;
}
else
{
// change references on the child
destChild.Parent = newParent;
destChild.ParentId = newParent.Id;
}
newParent.Children.Add(destChild);
}
}
}
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.