使用AutoMapper在Entity Framework中嵌套查询

时间:2018-03-22 08:37:55

标签: c# entity-framework automapper

用例如下:客户帐户只有一个地址,包括运费或结算。我需要将它合并到一个Address实体中。除此之外,地址行是数据库中的单独列,但必须是DTO中的IEnumerable字符串。这一直必须支持ProjectTo(),因为必须实现OData。如何在不更改数据库的情况下完成此操作?由于遗留应用程序,我无法触摸它。

第一个代码示例正常工作,而第二个代码示例失败:The nested query is not supported. Operation1='Case' Operation2='Collect'。这可能是因为它必须创建子实体。我怎样才能让它发挥作用?是否有任何解决方法(可能重组我的实体)?

在第一个示例中,我将不同地址行列的映射添加到List<string>。然后在调用时,.Include()实体框架确切地知道该做什么并且它可以工作。

public void Main()
{
    Mapper.Initialize(cfg =>
    {
        cfg.CreateMap<ShippingAddress, DtoShippingAddress>
            .ForMember(dto => dto.AddressLines,
                entity => entity.MapFrom(source => new List<string>
                    {
                        source.AddressLine1,
                        source.AddressLine2,
                        source.AddressLine3
                    }));

        cfg.CreateMap<BillingAddress, DtoBillingAddress>
            .ForMember(dto => dto.AddressLines,
                entity => entity.MapFrom(source => new List<string>
                    {
                        source.AddressLine1,
                        source.AddressLine2,
                        source.AddressLine3
                    }));
    });
    Mapper.AssertConfigurationIsValid();
    var context = new ClientContext();
    var result = context.CustomerAccounts.Include(i => i.ShippingAddress).Include(i => i.BillingAddress).ProjectTo<DtoCustomerAccount>();//.Dump();
}

public class CustomerAccount
{
    public int Id { get; set; }

    public ShippingAddress ShippingAddress { get; set; }

    public BillingAddress BillingAddress { get; set; }
}

public class ShippingAddress
{
    public int Id { get; set; }

    public string AddressLine1 { get; set; }

    public string AddressLine2 { get; set; }

    public string AddressLine3 { get; set; }
}

public class BillingAddress
{
    public int Id { get; set; }

    public string AddressLine1 { get; set; }

    public string AddressLine2 { get; set; }

    public string AddressLine3 { get; set; }
}

public class DtoCustomerAccount
{
    public int Id { get; set; }

    public DtoShippingAddress ShippingAddress { get; set; }

    public DtoBillingAddress BillingAddress { get; set; }
}

public class DtoShippingAddress
{
    public int Id { get; set; }

    public List<string> AddressLines { get; set; }
}

public class DtoBillingAddress
{
    public int Id { get; set; }

    public List<string> AddressLines { get; set; }
}

public class Initializer : DropCreateDatabaseAlways<ClientContext>
{       
    protected override void Seed(ClientContext context)
    {
        context.CustomerAccounts.Add(new UserQuery.CustomerAccount {ECommercePublished=true,  Articles = new []{ new Article{IsDefault=true, NationId=1, ProductId=1}}});
    }
}

public class ClientContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    Database.Log = s=>Debug.WriteLine(s);
        Database.SetInitializer(new Initializer());
    }

    public DbSet<CustomerAccount> CustomerAccounts { get; set; }
}

我需要它实现的方式失败了:

//The nested query is not supported. Operation1='Case' Operation2='Collect'
public void Main()
{
    Mapper.Initialize(cfg =>
    {
        cfg.CreateMap<CustomerAccount, DtoCustomerAccount>
            .ForMember(dto => dto.Address,
                entity => entity.MapFrom(source =>
                    source.BillingAddress == null
                        ? new Address
                        {
                            Id = source.ShippingAddress.Id,
                            AddressLines = new [] // or new List<string>
                            {
                                source.ShippingAddress.AddressLine1,
                                source.ShippingAddress.AddressLine2,
                                source.ShippingAddress.AddressLine3
                            }
                        }
                        : new Address
                        {
                            Id = source.BillingAddress.Id,
                            AddressLines = new [] // or new List<string>
                            {
                                source.BillingAddress.AddressLine1,
                                source.BillingAddress.AddressLine2,
                                source.BillingAddress.AddressLine3
                            }
                        }));
    });
    Mapper.AssertConfigurationIsValid();
    var context = new ClientContext();
    var result = context.CustomerAccounts.ProjectTo<DtoCustomerAccount>();//.Dump();
}

public class CustomerAccount
{
    public int Id { get; set; }

    public ShippingAddress ShippingAddress { get; set; }

    public BillingAddress BillingAddress { get; set; }
}

public class ShippingAddress
{
    public int Id { get; set; }

    public string AddressLine1 { get; set; }

    public string AddressLine2 { get; set; }

    public string AddressLine3 { get; set; }
}

public class BillingAddress
{
    public int Id { get; set; }

    public string AddressLine1 { get; set; }

    public string AddressLine2 { get; set; }

    public string AddressLine3 { get; set; }
}

public class DtoCustomerAccount
{
    public int Id { get; set; }

    public DtoAddress Address { get; set; }
}

public class DtoAddress
{
    public int Id { get; set; }

    public List<string> AddressLines { get; set; }
}

public class Initializer : DropCreateDatabaseAlways<ClientContext>
{       
    protected override void Seed(ClientContext context)
    {
        context.CustomerAccounts.Add(new UserQuery.CustomerAccount {ECommercePublished=true,  Articles = new []{ new Article{IsDefault=true, NationId=1, ProductId=1}}});
    }
}

public class ClientContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    Database.Log = s=>Debug.WriteLine(s);
        Database.SetInitializer(new Initializer());
    }

    public DbSet<CustomerAccount> CustomerAccounts { get; set; }
}

1 个答案:

答案 0 :(得分:2)

从技术上讲,它不是AutoMapper问题,因为ProjectTo只是使用您提供的表达式。在这种情况下,使用List<string>的EF查询技巧不能与条件运算符一起使用,因为BillingAddressShippingAddress是导航属性,EF翻译器正在尝试使用不能查询的子查询与条件运算符结合使用。因此,您需要找到与EF查询兼容的方式来指定所需的投影。

一种可能的方法是对每个结果地址属性使用条件运算符:

.ForMember(dto => dto.Address, entity => entity.MapFrom(source => new DtoAddress
{
    Id = source.BillingAddress != null ? source.BillingAddress.Id : source.ShippingAddress.Id,
    AddressLines = new List<string>
    {
        source.BillingAddress != null ? source.BillingAddress.AddressLine1 : source.ShippingAddress.AddressLine1,
        source.BillingAddress != null ? source.BillingAddress.AddressLine2 : source.ShippingAddress.AddressLine2,
        source.BillingAddress != null ? source.BillingAddress.AddressLine3 : source.ShippingAddress.AddressLine3,
    }   
}))