AutoMapper:将字符串转换为Object?

时间:2015-04-08 20:31:01

标签: c# nhibernate xsd automapper

我正在开发的项目使用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工具创建上面的类。但我在这里简化了它。

模型类是:

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

当我尝试保存Person信息时,我收到错误:

  

无法执行批处理命令。[SQL:SQL不可用]
  无法将值NULL插入列“fkLanguageID

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

修改 我意识到我需要将 Person.languageId 值更改为 PersonModel.Language 对象,以便NHibernate可以读取并映射它。是否可以使用客户解析器或类型转换器来实现此目的?

这就是我想要做的事情:

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)
        {
            ?????
        }
    }

1 个答案:

答案 0 :(得分:1)

我通过创建AutoMapper为复杂映射提供的解析器解决了这个问题。在创建映射时,我告诉映射器通过将其解析为Language类型的对象来解析具有源语言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);
    }
}