我必须用稍微不同的模式将一组数据从一个数据库导入到另一个数据库,并且我正在考虑使用AutoMap。我可以编写一堆SQL脚本,但我已经在EF中拥有了这两个数据库,我想学习AutoMap .
虽然许多类是相似的,但我遇到的问题是结构实际上是不同的。目标模型被设计成多个层次的类。我需要扩张,而不是扁平化。
目标类具有以下属性:
public class Account
{
public int Id { get; set; }
public string Name { get; set; }
public ContactInfo Location { get; set; }
public List<Policy> Policies { get; set; }
}
public class ContactInfo
{
public int Id { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public State State { get; set; }
public string Zip { get; set; }
public string EMail { get; set; }
public List<Phone> PhoneNumbers { get; set; }
}
public class Phone
{
public int Id { get; set; }
public string Name { get; set; }
public string Number { get; set; }
}
public class Policy
{
public int Id { get; set; }
public PolicyNumber PolicyNumber { get; set; }
public List<Transaction> Transactions { get; set; }
}然而,源表是相对平坦的。
public partial class Account
{
public string AccountId { get; set; }
public string AccountName { get; set; }
public string PolicyNumber { get; set; }
}
public partial class Transaction
{
public int TransactionId { get; set; }
public int AccountId { get; set; }
public Nullable<System.DateTime> EffectiveDate { get; set; }
public string InsuredName { get; set; }
public string InsuredAddress { get; set; }
public string InsuredCity { get; set; }
public string InsuredState { get; set; }
public string InsuredZip { get; set; }
public string InsuredPhone { get; set; }
}我可以创建映射,但我不知道如何告诉AutoMapper如何处理将string转换为策略对象,然后将其添加到策略列表中。
Mapper.CreateMap<Source.Account, Destination.Account>();更糟糕的是,源数据在事务级别上不显式地具有名称和地址信息。在您告诉我AutoMap可能不是最好的解决方案之前,请理解这两个源表是这个数据库中40多个表中的2个,其他的几乎没有那么麻烦。
我能否将AutoMap配置为将string属性PolicyNumber转换为策略对象并将其添加到目标类的策略列表中?
对于如何将事务中的名称和地址信息输入到ContactInfo类并将其添加到帐户级别,有什么建议吗?
谢谢。
发布于 2013-12-24 21:24:27
多亏了托马斯·韦勒。自定义值解析器处理的正是我所需要的。
https://stackoverflow.com/questions/20737839
复制相似问题