Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.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# 如何使用automapper映射多对多_C#_Asp.net Mvc_Entity Framework 6_Automapper_Dto - Fatal编程技术网

C# 如何使用automapper映射多对多

C# 如何使用automapper映射多对多,c#,asp.net-mvc,entity-framework-6,automapper,dto,C#,Asp.net Mvc,Entity Framework 6,Automapper,Dto,我有多对多关系表函数和角色,我有关联表角色函数。 在RolesFunctions中,我有行id(IdFunctions和IdRoles都是一个主键)问题是,当我将函数与functionDTO映射,将角色与rolesDTO映射到AutoMapper时,出现以下错误:“AutoMapper.dll中发生了类型为'System.StackOverflowException'的未处理异常。请确保没有无限循环或无限递归。” 我的映射: CreateMap<Function, FunctionTDTO

我有多对多关系表函数和角色,我有关联表角色函数。 在RolesFunctions中,我有行id(IdFunctions和IdRoles都是一个主键)问题是,当我将函数与functionDTO映射,将角色与rolesDTO映射到AutoMapper时,出现以下错误:“AutoMapper.dll中发生了类型为'System.StackOverflowException'的未处理异常。请确保没有无限循环或无限递归。”

我的映射:

CreateMap<Function, FunctionTDTO>().ReverseMap();
CreateMap<Roles, RolesDTO>().ReverseMap();
CreateMap().ReverseMap();
CreateMap().ReverseMap();
automapper如何映射多对多关系

函数类

public class Functions
    {
        public Functions()
        {
            this.Roles = new HashSet<Roles>();
        }
        public int Id { get; set; }
        public string Description { get; set; }
        public virtual ICollection<Roles> Roles { get; set; }
    }
公共类函数
{
公共职能()
{
this.Roles=new HashSet();
}
公共int Id{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection角色{get;set;}
}
函数DTO

public class FunctionsDTO
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public virtual ICollection<RolesDTO> Roles { get; set; }
    }
公共类函数到
{
公共int Id{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection角色{get;set;}
}
角色类别

public class Roles
    {
        public Roles()
        {
            this.Functions = new HashSet<Functions>();
        }
        public int Id { get; set; }
        public string Libelle { get; set; }
        public virtual ICollection<Functions> Functions { get; set; }
    }
公共类角色
{
公共角色()
{
this.Functions=new HashSet();
}
公共int Id{get;set;}
公共字符串Libelle{get;set;}
公共虚拟ICollection函数{get;set;}
}
角色分类

public class RolesDTO
    {
        public int Id { get; set; }
        public string Libelle { get; set; }
        public virtual ICollection<FunctionsDTO> Functions { get; set; }
    }
公共类角色DTO
{
公共int Id{get;set;}
公共字符串Libelle{get;set;}
公共虚拟ICollection函数{get;set;}
}

在一个DTO到第二个DTO中具有引用不是一个好的做法,因为它会导致许多问题(例如,这)。您应该根据您的用例制作DTO。例如:

public class FunctionsDTO
{
    public int Id { get; set; }
    public string Description { get; set; }
    public virtual ICollection<RolesDTO> Roles { get; set; }
}
公共类函数到
{
公共int Id{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection角色{get;set;}
}
当您只需要使用Roles.Libelle(例如在显示或创建中)时,您应该将此DTO作为示例:

public class FunctionsCreateDTO
{
    public int Id { get; set; }
    public string Description { get; set; }
    public virtual ICollection<string> RoleLibelles { get; set; }
}
public类函数创建到
{
公共int Id{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection RoleLibelles{get;set;}
}

然后只需更改映射。

向我们显示类Function、FunctionDTO、Roles和RolesDTO