Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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#_Entity Framework_Automapper - Fatal编程技术网

C# 使用AutoMapper映射父/子接口属性

C# 使用AutoMapper映射父/子接口属性,c#,entity-framework,automapper,C#,Entity Framework,Automapper,我在标准层次结构中使用了以下接口和DTO: public interface IPageView { IPageView Parent { get; set; } ICollection<IPageView> Children { get; set; } } 当AutoMapper尝试创建映射时,出现以下异常: 无法映射Pipeline.CMS.Contracts.UI.IPageView上的以下属性: 页 添加自定义映射表达式、忽略、添加自定义解析程序或修改目标类

我在标准层次结构中使用了以下接口和DTO:

public interface IPageView {
    IPageView Parent { get; set; }
    ICollection<IPageView> Children { get; set; }
}
当AutoMapper尝试创建映射时,出现以下异常:

无法映射Pipeline.CMS.Contracts.UI.IPageView上的以下属性: 页 添加自定义映射表达式、忽略、添加自定义解析程序或修改目标类型Pipeline.CMS.Contracts.UI.IPageView。 背景: 从Model.Page映射到Interfaces.IPageView的属性页 从System.Collections.Generic.ICollection
1[[Model.Page,Model,Version=1.0.0,Culture=neutral,PublicKeyToken=null]]映射到属性页到System.Collections.Generic.ICollection
1[[Interfaces.IPageView,Interfaces,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]] 从类型ModelPage到Interfaces.IPageView的映射 引发了类型为“AutoMapper.AutoMapperConfigurationException”的异常

显然这是因为AutoMapper无法映射IPageView,因为它尚未映射


我的问题是如何解决这个问题?我不介意在AutoMapper配置中指定一个具体的类。

上面的代码似乎没有问题,问题与层次结构无关,但是需要在原始源对象和
As
方法中的对象之间创建一个映射。对于上述情况,将是:

AutoMapper.Mapper.CreateMap<Page, PageView>(); // Additional line here needed
AutoMapper.Mapper.CreateMap<Page, IPageView>().As<PageView>();
AutoMapper.Mapper.CreateMap();//这里需要额外的线路
AutoMapper.Mapper.CreateMap().As();
AutoMapper.Mapper.CreateMap<Page, IPageView>().As<PageView>();
AutoMapper.Mapper.CreateMap<Page, PageView>(); // Additional line here needed
AutoMapper.Mapper.CreateMap<Page, IPageView>().As<PageView>();