自动映射:映射层次结构实体

时间:2015-01-16 10:01:59

标签: c# automapper

我有Entity模型,包含User字段和Parent(相同类型)。

class User {
    public int Id { get;set; }
    public string Name {get;set;}
}
class Entity {
    public int Id {get;set;}
    public int ParentId {get;set;}
    public virtual User user { get;set; }
    public virtual Entity Parent {get; set;}
}

class UserModel {
    public string Name {get;set;}
}

class EntityModel {
    public UserModel { get;set; }
    public EntityParentModel { get; set; }
}

class EntityParentModel {
    public UserModel { get;set; }
}

我尝试使用AutoMapper将Entity映射到EntityModel,但收到以下错误:

  

“UserModel”类型出现在单个LINQ to Entities查询中的两个结构不兼容的初始化中。可以在同一查询中的两个位置初始化类型,但前提是在两个位置都设置了相同的属性,并且这些属性的设置顺序相同。

那么,有人知道我需要做什么吗?如果需要,我可以提供映射配置。

UP:项目功能很简单:

private static readonly Func<IQueryable<TEntity>, string[], IQueryable<TModel>> ProjectFunction = (query, expand) => query.Project().To<TModel>(membersToExpand: expand);

配置在这里:

CreateMap<User, UserModel>();
CreateMap<Entity, EntityModel>();
CreateMap<Entity, EntityParentModel>();

0 个答案:

没有答案