AutoMapper:将字符串转换为 Object?

AutoMapper: conversion of string into Object?

我正在处理的项目使用 NHibernate、AutoMapper 和 Fluent。我需要做的就是读取 XML 文件并将数据输入数据库。但是我面临一个问题。当我尝试映射源和目标时,出现标题中提到的错误。

下面是我的代码:

public partial class Language
{
    public string languageIdField;
    public string languageNameField;
}

 public partial class Person
        {   
            public int personIdField;  
            public string firstNameField;  
            public string lastNameField;  
            public int stateField;
            public int enableEmailField;
            public int attestPersonLockedField;
            public string emailAddressField;
            public string languageId;
         }

我在上面 类 使用 xsd2code 创建 tool.But 我在这里简化了它。

型号类是:

public class Person
{
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string EmailAddress {get; set;}
    .....
    public int state {get; set;}

    public virtual User User { get; set; }
    public virtual Language language { get; set; }
}

public class Language
{
    public virtual string LanguageId { get; set; }
    public virtual string LanguageName { get; set; }
    public virtual IList<Person> Persons { get; set; }
}

这就是我使用 AutoMapper 映射它们的方式:

AutoMapper.Mapper.CreateMap<Language, Models.Language>();
AutoMapper.Mapper.CreateMap<Person, Models.Person>();

这就是我阅读和尝试保存数据的方式:

object id = null;
foreach (var item in templateData.Languages)
{
    id = save<Dicom.Expense.Models.Language>(item); // this will return the language id
}

Person person = new Person();
person.Emailaddress = templatedata.Person.EmailAddress;
....
person.languageId = id.ToString();  
save<Dicom.Expense.Models.Person>(person);
private void save<TModel>(object templateObject)
{
   var dbModel = AutoMapper.Mapper.Map<TModel>(templateObject);
   repository.Save<object>(dbModel);
}

当我尝试保存个人信息时出现错误:

could not execute batch command.[SQL: SQL not available]
Cannot insert the value NULL into column 'fkLanguageID

这是因为 Person table 将语言 ID 作为外键。现在我不知道我需要做什么更改才能正确映射 Person 源和目标并将数据保存到数据库中。

编辑: 我意识到我需要将 Person.languageId 值更改为 PersonModel.Language object 以便 NHibernate 可以阅读并绘制地图。是否可以使用 Customer Resolver 或 Type Converter 来实现此目的?

这就是我想要做的:

AutoMapper.Mapper.CreateMap<Person, Models.Person>().ForMember(dest => dest.Language, opt => opt.ResolveUsing<CustomResolver>());

public class CustomResolver : ValueResolver<Person, Models.Language>
    {
        public Dicom.Expense.Models.Language ResolveCore(Person source)
        {
            ?????
        }
    }

我通过创建一个 AutoMapper 为复杂映射提供的解析器解决了这个问题。在创建映射时,我告诉映射器通过将目标语言解析为语言类型的对象来解析带有源语言 ID 的目标语言。

下面是我的代码;

AutoMapper.Mapper.CreateMap<Person, Models.Person>()
                .ForMember(x => x.Language, opt => opt.ResolveUsing(new LanguageCodeResolver(loadRepository)).FromMember(x => x.LanguageId));

public class LanguageCodeResolver : ValueResolver<string, Dicom.Expense.Models.Language>
{
    private IDatabaseLoadRepository loadRepository;
    public LanguageCodeResolver(IDatabaseLoadRepository loadRepository)
    {
        this.loadRepository = loadRepository;
    }
    protected override Models.Language ResolveCore(string languageCode)
    {
        return loadRepository.FindOne<Models.Language>(x => x.LanguageId == languageCode);
    }
}