Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何映射列表<;int>;使用Automapper转换为复杂类型的属性?_C#_Mapping_Automapper - Fatal编程技术网

C# 如何映射列表<;int>;使用Automapper转换为复杂类型的属性?

C# 如何映射列表<;int>;使用Automapper转换为复杂类型的属性?,c#,mapping,automapper,C#,Mapping,Automapper,我有这个数据模型: public class ObjectType { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string Name { get; set; } public virtual ICollection<ObjectType> AllowedSubTypes { get; set

我有这个数据模型:

public class ObjectType
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<ObjectType> AllowedSubTypes { get; set; }
}
public class ObjectTypeDto
{
    public int Id { get; set; }

    public string Name { get; set; }

    public IList<int> AllowedTypes { get; set; }
}
公共类ObjectType
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection允许子类型{get;set;}
}
我需要使用Automapper将以下DTO模型映射到上述数据模型中:

public class ObjectType
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<ObjectType> AllowedSubTypes { get; set; }
}
public class ObjectTypeDto
{
    public int Id { get; set; }

    public string Name { get; set; }

    public IList<int> AllowedTypes { get; set; }
}
公共类ObjectTypeDto
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共IList允许的类型{get;set;}
}

我的问题是如何将integer列表映射到仅用于Id属性的List AllowedSubTypes中?

您必须创建从
int
List
ObjectType
List
的特定映射,并在数据模型和DTO类之间创建映射

x.CreateMap<ObjectTypeDto, ObjectType>()
    .ForMember(m => m.AllowedSubTypes, mv => mv.MapFrom(m => m.AllowedTypes));

x.CreateMap<int, ObjectType>()
    .ForMember(m => m.Id, mv => mv.MapFrom(m => m));
x.CreateMap()
.ForMember(m=>m.AllowedSubTypes,mv=>mv.MapFrom(m=>m.AllowedTypes));
x、 CreateMap()
.ForMember(m=>m.Id,mv=>mv.MapFrom(m=>m));

谢谢您的回答。我已经尝试过了,但是我遇到了这个异常“缺少类型映射配置或不支持的映射”