AutoMapper 4 映射错误

AutoMapper 4 mapping Error

我有两个 class。一个是名为 "ImportedContact" 的 class,它映射到 csv 文件中的记录。对于文件中的每一行,都有一个 class 的实例。我们使用 LINQtoCSV 库将值检索到此 class。第二个是名为 "Contact" 的 class,与 Telerik 的 DataAccess ORM 库一起使用。因此,我们的过程是使用 LINQtoCSV 读取 csv 文件,填充 ImportedContact class 实例并将它们分别映射到 Contact class 实例中,我们将它们添加到数据库上下文并更新我们的数据库。当我们在这些 classes 之间映射时,我得到一个对我来说没有意义的异常。

这是 ImportedContact class:

using System;
using System.Linq;
using LINQtoCSV;

namespace SharePointDirectory.Jobs
{
    public class ImportedContact
    {
        [CsvColumn(Name = "Location Name", FieldIndex = 4)]
        public string BranchId
        {
            get;
            set;
        }

        [CsvColumn(Name = "Work Wireless", FieldIndex = 7)]
        public string CellNumber
        {
            get;
            set;
        }

        [CsvColumn(Name = "Home Department Name", FieldIndex = 3)]
        public string Dept
        {
            get;
            set;
        }

        [CsvColumn(Name = "Extension", FieldIndex = 9)]
        public string Ext
        {
            get;
            set;
        }

        [CsvColumn(Name = "First Name", FieldIndex = 2)]
        public string FirstName
        {
            get;
            set;
        }

        [CsvColumn(Name = "Last Name", FieldIndex = 1)]
        public string LastName
        {
            get;
            set;
        }

        [CsvColumn(Name = "Shift -- Value", FieldIndex = 5)]
        public string Shift
        {
            get;
            set;
        }

        [CsvColumn(Name = "Job Title", FieldIndex = 8)]
        public string Title
        {
            get;
            set;
        }

        [CsvColumn(Name = "Work Phone", FieldIndex = 6)]
        public string WorkNumber
        {
            get;
            set;
        }
    }
}

这是联系方式class:

using System;
using System.Linq;
using System.Runtime.Serialization;

namespace SharePointDirectory.Data  
{
    [Serializable]
    public class Contact : ISerializable
    {
        public Contact()
        {
        }

        protected Contact(SerializationInfo info, StreamingContext context)
        {
            this.Id = info.GetInt64("Id");
            this.EmployeeId = (int?)info.GetValue("EmployeeId", typeof(int?));
            this.FirstName = info.GetString("FirstName");
            this.LastName = info.GetString("LastName");
            this.Title = info.GetString("Title");
            this.Ext = info.GetString("Ext");
            this.Dept = info.GetString("Dept");
            this.DeptId = info.GetString("DeptId");
            this.CellNumber = info.GetString("CellNumber");
            this.PhotoPath = info.GetString("PhotoPath");
            this.Active = info.GetBoolean("Active");
            this.Office = info.GetBoolean("Office");
            this.Shift = info.GetString("Shift");
            this.BranchId = info.GetString("BranchId");
            this.WorkNumber = info.GetString("WorkNumber");
        }

        public bool Active
        {
            get;
            set;
        }

        public string BranchId
        {
            get;
            set;
        }

        public string CellNumber
        {
            get;
            set;
        }

        public string Dept
        {
            get;
            set;
        }

        public string DeptId
        {
            get;
            set;
        }

        public int? EmployeeId
        {
            get;
            set;
        }

        public string Ext
        {
            get;
            set;
        }

        public string FirstName
        {
            get;
            set;
        }

        public long Id
        {
            get;
            set;
        }

        public string LastName
        {
            get;
            set;
        }

        public bool Office
        {
            get;
            set;
        }

        public string PhotoPath
        {
            get;
            set;
        }

        public string Shift
        {
            get;
            set;
        }

        public string Title
        {
            get;
            set;
        }

        public string WorkNumber
        {
            get;
            set;
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Id", this.Id, typeof(long));
            info.AddValue("EmployeeId", this.EmployeeId, typeof(int?));
            info.AddValue("FirstName", this.FirstName, typeof(string));
            info.AddValue("LastName", this.LastName, typeof(string));
            info.AddValue("Title", this.Title, typeof(string));
            info.AddValue("Ext", this.Ext, typeof(string));
            info.AddValue("Dept", this.Dept, typeof(string));
            info.AddValue("DeptId", this.DeptId, typeof(string));
            info.AddValue("CellNumber", this.CellNumber, typeof(string));
            info.AddValue("PhotoPath", this.PhotoPath, typeof(string));
            info.AddValue("Active", this.Active, typeof(bool));
            info.AddValue("Office", this.Office, typeof(bool));
            info.AddValue("Shift", this.Shift, typeof(string));
            info.AddValue("BranchId", this.BranchId, typeof(string));
            info.AddValue("WorkNumber", this.WorkNumber, typeof(string));
        }
    }
}

以下是我们如何设置映射:

Mapper.Initialize(configuration => configuration.CreateMap<ImportedContact, Contact>());

以下是我们在运行时执行实际地图的方式:

var contact = Mapper.Map<Contact>(importedContact);

当我们执行映射时,这是我们看到的异常消息:

{
    "Missing type map configuration or unsupported mapping.

    Mapping types:
    ImportedContact -> SerializationInfo
    SharePointDirectory.Jobs.ImportedContact -> System.Runtime.Serialization.SerializationInfo

    Destination path:
    Contact

    Source value:
    SharePointDirectory.Jobs.ImportedContact"
}   

System.Exception {AutoMapper.AutoMapperMappingException}

我不明白为什么要对此处列出的这两种类型执行映射。 SerializationInfo 是其中一个构造函数的构造函数参数,但我认为还应该注意一个无参数构造函数,而不是相反。同样的代码似乎在 AutoMapper 3.3 版中也能正常工作。

我一直在查看 AutoMapper 文档以查看是否可以找到任何内容。目前最接近的就是提到这两件事

https://github.com/AutoMapper/AutoMapper/wiki/Construction

https://github.com/AutoMapper/AutoMapper/wiki/Configuration

具体关于配置前缀的说明:

By default AutoMapper recognizes the prefix "Get"

我不确定自动映射器的 never 版本,但我认为,您仍然需要指定目标映射 class,否则它不知道映射到哪里。

var contact = Mapper.Map<ImportedContact, Contact>(importedContact);

我有一个类似的问题,因为 AutoMapper 决定使用带参数的构造函数而不是默认构造函数。

我的解决方法是自己实例化目标对象并使 AutoMapper 填写属性:

var contact = new Contact();
Mapper.Map(importedContact, contact);