Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.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_Automapper - Fatal编程技术网

C# 如何使用Automapper完成以下映射?

C# 如何使用Automapper完成以下映射?,c#,asp.net-mvc,entity-framework,automapper,C#,Asp.net Mvc,Entity Framework,Automapper,我想映射类(实体) 到以下类(视图模型): 其中,MyClass1、MyClass2的定义如下: public class MyClass1 { public int x; public string y; public bool z; } public class MyClass2 { public int a; public int b; public int c; } Automapper有可能做到这一点吗?Automapper手柄变平,这似乎是您在

我想映射类(实体)

到以下类(视图模型):

其中,MyClass1、MyClass2的定义如下:

public class MyClass1 {
   public int x;
   public string y;
   public bool z;
}

public class MyClass2 {
   public int a;
   public int b;
   public int c;
}

Automapper有可能做到这一点吗?

Automapper手柄变平,这似乎是您在这里要求的。不过,它的方向与通常的方向相反,所以它可能会窒息。如果有,您可以使用单个
.ForMember()
映射来处理它

在您的情况下,它看起来如下所示:

Mapper.CreateMap<Source, Destination>()
    .ForMember(cv => cv.Destination, m => m.MapFrom(
    s => new Class1(s.x, s.y, s.z))
Mapper.CreateMap()
.FormMember(cv=>cv.Destination,m=>m.MapFrom(
s=>新类别1(s.x、s.y、s.z))

我现在无法在一个环境中运行它,所以请指出任何语法错误,我会纠正它们。

刚刚测试了这个,它可以独立工作

        Mapper.CreateMap<Source, MyClass1>();
        Mapper.CreateMap<Source, MyClass2>();

        Mapper.CreateMap<Source, Destination>()
            .ForMember(x => x.A, m => m.MapFrom(p => p))
            .ForMember(x => x.B, m => m.MapFrom(p => p));


        var source = new Source() { a = 1, b = 2, c = 3, x = 4, y = "test", z = true };
        var destination = new Destination() { A = new MyClass1(), B = new MyClass2() };
        Mapper.Map<Source, Destination>(source, destination);
Mapper.CreateMap();
CreateMap();
Mapper.CreateMap()
.ForMember(x=>x.A,m=>m.MapFrom(p=>p))
.ForMember(x=>x.B,m=>m.MapFrom(p=>p));
var source=new source(){a=1,b=2,c=3,x=4,y=“test”,z=true};
var destination=new destination(){A=new MyClass1(),B=new MyClass2()};
Mapper.Map(源、目的地);

我的答案与此相同。唯一的区别是我没有首先创建新的目的地类。
var Destination=Mapper.Map(source);
Mapper.CreateMap<Source, Destination>()
    .ForMember(cv => cv.Destination, m => m.MapFrom(
    s => new Class1(s.x, s.y, s.z))
        Mapper.CreateMap<Source, MyClass1>();
        Mapper.CreateMap<Source, MyClass2>();

        Mapper.CreateMap<Source, Destination>()
            .ForMember(x => x.A, m => m.MapFrom(p => p))
            .ForMember(x => x.B, m => m.MapFrom(p => p));


        var source = new Source() { a = 1, b = 2, c = 3, x = 4, y = "test", z = true };
        var destination = new Destination() { A = new MyClass1(), B = new MyClass2() };
        Mapper.Map<Source, Destination>(source, destination);