Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通用对象到带有Automapper的通用对象_C#_.net_Entity Framework_Automapper - Fatal编程技术网

C# 通用对象到带有Automapper的通用对象

C# 通用对象到带有Automapper的通用对象,c#,.net,entity-framework,automapper,C#,.net,Entity Framework,Automapper,我在将泛型对象复制到泛型对象时遇到问题 public class Customer { public int CustomerId { get; set; } public virtual ICollection<Quote> Quotes { get; set; } } 公共类客户 { public int CustomerId{get;set;} 公共虚拟ICollection引号{get;set;} } 我使用此泛型类将对象复制到对象: public sta

我在将泛型对象复制到泛型对象时遇到问题

public class Customer
{
    public int CustomerId { get; set; }
    public virtual ICollection<Quote> Quotes { get; set; }
}
公共类客户
{
public int CustomerId{get;set;}
公共虚拟ICollection引号{get;set;}
}
我使用此泛型类将对象复制到对象:

public static class GenericAutomapper
{
    public static void PropertyMap<T, U>(T source, U destination)
        where T : class, new()
        where U : class, new()
    {
        Mapper.CreateMap(typeof(T), typeof(U));
        Mapper.Map<T, U>(source); //crash here
    }
}
公共静态类通用自动映射器
{
公共静态void属性映射(T源,U目标)
其中T:class,new()
其中U:class,new()
{
CreateMap(typeof(T),typeof(U));
Mapper.Map(源);//在这里崩溃
}
}
当我得到一个客户(使用EF 6.1.2)并使用此方法时,我在“crash here”行中出现错误。Quotes集合如下所示: “((System.Data.Entity.DynamicProxies.Customer_AC635AD71AC95634EF9694FDC434135B488FD116F3C2B6A287846A788652F3F)来源)。Quotes”

当我包含以下内容时,我没有任何问题:
.include(x=>x.Quotes)
在我的查询中,通常会加载集合

是否有办法管理“未加载”集合


谢谢,

您需要在映射之前关闭延迟加载或执行查询;调用.Include()有助于实现这一点,或者您需要在应用映射之前调用.ToList()(或类似的内容)。

此类必须保持通用性,当然对类没有任何特定性。映射类将保持特定性,但问题是您试图映射动态代理类,这不是一个好主意。您需要执行查询(这将不是通用的,因为我希望您将处理特定的dbset),然后调用AutoMapper映射函数。这将为您提供具体的类型,并应允许映射发挥作用。在提供的信息有限的情况下,这是最好的猜测。