AutoMapper 4映射错误

时间:2015-08-14 17:16:52

标签: c# serialization orm automapper poco

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

这是ImportedContact类:

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;
        }
    }
}

以下是Contact类:

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"

1 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,AutoMapper决定使用带有参数的构造函数而不是默认的构造函数。

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

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