【发布时间】:2017-12-21 10:11:13
【问题描述】:
关于我的博客数据模型:
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Text { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
另外,相应的 DTO 也是这样定义的:
public class BlogDto
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<PostDto> Posts { get; set; }
}
public class PostDto
{
public int Id { get; set; }
public string Text { get; set; }
public int BlogId { get; set; }
public BlogDto Blog { get; set; }
}
然后,映射被初始化:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Blog, BlogDto>();
cfg.CreateMap<Post, PostDto>();
});
最后,我尝试使用 Automapper.EF6 将 Posts 投影到 PostDtos:
List<PostDto> result = null;
using (var db = new BloggingContext())
{
result = db.Set<Post>().ProjectToList<PostDto>();
}
但是我遇到了这个错误:
The type 'AutoMapperSample.Model.PostDto' appears in two structurally
incompatible initializations within a single LINQ to Entities query. A type
can be initialized in two places in the same query, but only if the same
properties are set in both places and those properties are set in the same
order.
这是生成的表达式:
.Call System.Linq.Queryable.Select(
.Call
.Constant<System.Data.Entity.Core.Objects.ObjectQuery`1[AutoMapperSample.Model.Post]>(System.Data.Entity.Core.Objects.ObjectQuery`1[AutoMapperSample.Model.Post]).MergeAs(.Constant<System.Data.Entity.Core.Objects.MergeOption>(AppendOnly))
,
'(.Lambda #Lambda1<System.Func`2[AutoMapperSample.Model.Post,AutoMapperSample.Model.PostDto]>))
.Lambda #Lambda1<System.Func`2[AutoMapperSample.Model.Post,AutoMapperSample.Model.PostDto]>(AutoMapperSample.Model.Post $dto)
{
.New AutoMapperSample.Model.PostDto(){
Id = $dto.Id,
Text = $dto.Text,
Blog = .If ($dto.Blog != null) {
.New AutoMapperSample.Model.BlogDto(){
Id = ($dto.Blog).Id,
Name = ($dto.Blog).Name,
Posts = .Call System.Linq.Enumerable.ToList(.Call System.Linq.Enumerable.Select(
($dto.Blog).Posts,
.Lambda #Lambda2<System.Func`2[AutoMapperSample.Model.Post,AutoMapperSample.Model.PostDto]>))
}
} .Else {
null
}
}
}
.Lambda #Lambda2<System.Func`2[AutoMapperSample.Model.Post,AutoMapperSample.Model.PostDto]>(AutoMapperSample.Model.Post $dto)
{
.New AutoMapperSample.Model.PostDto(){
Id = $dto.Id,
Text = $dto.Text
}
}
我需要知道结构是否错误,例如循环,或者需要考虑其他问题。
【问题讨论】:
-
嗯,可能解决不了
PostDto.Blog和BlogDto.Posts之间的循环引用。如果您.Ignore()CreateMap中的这些成员之一,它是否有效? -
@GeorgPatscheider 是的,如果我将 IgnoreMap 属性放在 BlogDto.Posts 上,或者如果我使用 MaxDepth(1),它会起作用。但我认为应该有一种适当的方法来保留这两个导航属性。另外,我尝试了 PreserveReferences 方法,又遇到了同样的错误。
标签: entity-framework automapper