Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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# 在.NET中自动映射嵌套类_C#_.net_Asp.net Core_Automapper - Fatal编程技术网

C# 在.NET中自动映射嵌套类

C# 在.NET中自动映射嵌套类,c#,.net,asp.net-core,automapper,C#,.net,Asp.net Core,Automapper,我有以下API类: public class ApiPlaceBetInput : ApiRequestBase { public ApiDoBet DoBet { get; set; } } public class ApiRequestBase { public ApiIdentity Identity { get; set; } } 我有业务逻辑类: public class BlPlaceBetInput { internal BlDoBetInput DoB

我有以下API类:

public class ApiPlaceBetInput : ApiRequestBase
{
    public ApiDoBet DoBet { get; set; }
}

public class ApiRequestBase
{
    public ApiIdentity Identity { get; set; }
}
我有业务逻辑类:

public class BlPlaceBetInput
{
    internal BlDoBetInput DoBet { get; set; }
    internal BlIdentity Identity { get; set; }
}
其中API和BL类都具有相同的属性名称。 如何在.NET中使用自动映射器将ApiceBetInput映射到BlPlaceBetInput?
如果属性名称不同,如何分别映射每个元素?

要将一个属性映射到另一个属性,需要使用选项

下面是一个将电话映射到电话的示例:

using AutoMapper;
using DataMapperDemo.Models;
using System;

namespace DataMapperDemp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var config = new MapperConfiguration(cfg => {
                cfg.CreateMap<Author, AuthorDTO>()
                .ForMember(destination => destination.Phone,
               opts => opts.MapFrom(source => source.Telephone));

            });
            IMapper iMapper = config.CreateMapper();
            Author author = new Author()
            {
                Id = 1,
                FirstName = "Chris",
                LastName = "Catignani",
                Address = new Address()
                { Street = "xxxxxxxxxxxxxxxxxxxxx",
                    City = "TN",
                    State = "TN",
                    Country = "USA"
                },
                Telephone = "(615) 474-7777"
            };
            var destination = iMapper.Map<Author, AuthorDTO>(author);
            Console.WriteLine(destination.FirstName + "," +
                                destination.LastName + "," +
                                destination.Street + "," +
                                destination.Phone + "," +
                                destination.Id);
        }
    }
}
使用AutoMapper;
使用DataMapperDemo.Models;
使用制度;
命名空间DataMapperDemp
{
班级计划
{
静态void Main(字符串[]参数)
{
控制台。WriteLine(“你好,世界!”);
var config=new-MapperConfiguration(cfg=>{
cfg.CreateMap()
.ForMember(destination=>destination.Phone,
opts=>opts.MapFrom(source=>source.Telephone));
});
IMapper IMapper=config.CreateMapper();
作者=新作者()
{
Id=1,
FirstName=“克里斯”,
LastName=“Catignani”,
地址=新地址()
{Street=“XXXXXXXXXXXXXXXXXX”,
City=“TN”,
State=“TN”,
Country=“美国”
},
电话=“(615)474-7777”
};
var destination=iMapper.Map(作者);
Console.WriteLine(destination.FirstName+“,”+
destination.LastName+“,”+
目的地。街道+“,”+
目的地。电话+“,”+
目的地(Id);
}
}
}

您尝试过什么?使用AutoMapper是正确的。AutoMapper名称中的主要内容是Auto。它应该自动完成它的工作。如果它不能自动完成它的工作,那么最好的解决方案就是放弃它,手动进行映射。对不起,这不是我想要的。您的phone和phone必须包含与我的示例相同的嵌套类。@Artavazd只需引用属性即可。destination.x.x到sourse.x.xi如果是这么简单的话,我会这么做。但它不是这样运作的。