【问题标题】:Expanding classes using AutoMap使用 AutoMap 扩展类
【发布时间】:2013-12-25 05:24:27
【问题描述】:

我必须使用稍微不同的架构将一组数据从一个数据库导入另一个数据库,我正在考虑使用 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 将字符串 Policy 转换为策略对象,然后将其添加到策略列表中。

Mapper.CreateMap<Source.Account, Destination.Account>();

更糟糕的是,源数据在事务级别不明确地包含名称和地址信息。在你告诉我 AutoMap 可能不是最好的解决方案之前,请理解这两个源表是这个数据库中 40 多个表中的 2 个,而其他的并没有那么麻烦。

能否配置 AutoMap 将字符串属性 PolicyNumber 转换为 Policy Object 并添加到目标类的 Policies List 中?

关于如何将姓名和地址信息从 Transaction 获取到 ContactInfo 类并将其添加到 Account 级别的任何建议?

谢谢。

【问题讨论】:

  • 它不能自动完成,但您可以轻松地为此插入Custom Value Resolver。或者您可以手动处理这两个表,而其余部分仍使用 AutoMapper。
  • 另一个问题,你要导入多少个实体? EF 中未实现对批量导入的默认支持,因此性能可能是个问题。

标签: c# automapper


【解决方案1】:

感谢托马斯·韦勒。自定义值解析器正好处理了我需要的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多