简体   繁体   English

无法使用OData,EF Core和AutoMapper映射List <>导航属性

[英]Unable to map List<> navigational properties using OData, EF Core and AutoMapper

I'm currently writing an ASP .NET Core API utilizing OData for querying, and Entity Framework to talk to the database. 我目前正在编写一个使用OData进行查询的ASP .NET Core API,并使用Entity Framework与数据库进行通讯。

I want to separate the domain objects from the DTOs sent to the user, so have also started to use AutoMapper to translate entity framework query results to DTOs I have created. 我想将域对象与发送给用户的DTO分开,因此也开始使用AutoMapper将实体框架查询结果转换为我创建的DTO。

At this point (while I'm testing), my DTOs and domain objects are the same - just public getter/setter properties. 在这一点上(我正在测试时),我的DTO和域对象是相同的-只是公共getter / setter属性。 Examples of the DTOs are as follows: DTO的示例如下:

    public class NoteDTO
    {
        public int Id { get; set; }
        public string Body { get; set; }
        public string Conclusion { get; set; }
        public string Title { get; set; }

        public ManagerDTO Manager { get; set; }
    }

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

        public virtual List<ProductDto> Products { get; set; }
    }

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

I also have a test method in my NotesController for fetching notes (again, using OData) which is as follows: 我的NotesController中也有一个测试方法,用于获取笔记(再次使用OData),如下所示:

        [HttpGet]
        [EnableQuery]
        public IQueryable<NoteDTO> GetMeeting()
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<Note, NoteDTO>();
                cfg.CreateMap<Product, ProductDto>();
                cfg.CreateMap<Manager, ManagerDTO>()
                    .ForMember(md => md.Products, conf => conf.MapFrom(m => m.Products));
            });
            return _context.Notes.ProjectTo<NoteDTO>(config);
        }

I then try and hit my API with the following query: 然后,我尝试使用以下查询命中我的API:

https://localhost:5001/api/Notes ?$select=Id,Body,Conclusion&$top=5&$expand=Manager($select=Id) https:// localhost:5001 / api / Notes ?$ select = Id,Body,Conclusion&$ top = 5&$ expand = Manager($ select = Id)

However, this fails, and in amongst the stack trace, I'm given the following error message: 但是,这失败了,并且在堆栈跟踪中,我得到以下错误消息:

System.ArgumentException: Expression of type 'System.Collections.Generic.IEnumerable`1[System.Tuple`3[TestEntityFramework.DataObjects.ProductDto,Microsoft.EntityFrameworkCore.Query.Internal.MaterializedAnonymousObject,Microsoft.EntityFrameworkCore.Query.Internal.MaterializedAnonymousObject]]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable`1[TestEntityFramework.DataObjects.ProductDto]' of method 'System.Collections.Generic.IEnumerable`1[TestEntityFramework.DataObjects.ProductDto] _ToEnumerable[ProductDto](System.Collections.Generic.IEnumerable`1[TestEntityFramework.DataObjects.ProductDto])'

If I remove the List from the ManagerDTO object and the relevant Product mapping config, the query above works successfully. 如果我从ManagerDTO对象和相关的产品映射配置中删除列表,则上面的查询将成功工作。

I saw this comment on a GitHub issue for what sounds like the same problem, but trying to implement the suggestion hasn't helped (assuming I've understood them correctly): https://github.com/AutoMapper/AutoMapper/issues/2853#issuecomment-482317381 我在GitHub问题上看到了类似问题的评论,但是尝试实施该建议并没有帮助(假设我正确理解了它们): https : //github.com/AutoMapper/AutoMapper/issues/ 2853#issuecomment-482317381

Has anyone else run into this problem? 还有其他人遇到这个问题吗? I'm still getting used to AutoMapper so may have missed something obvious, but from searching around this seems to be a fairly uncommon issue and so pointers as to what's going on here have been hard to come by. 我仍然习惯于使用AutoMapper,因此可能错过了一些显而易见的事情,但是从周围进行搜索似乎是一个相当不常见的问题,因此很难找到关于这里发生的事情的指针。

I'm open to any other suggestions as to what the best way of translating an OData query to entity framework, then back to a DTO is as well - if what I'm doing here isn't optimal! 对于将OData查询转换为实体框架,然后再转换为DTO的最佳方法,我还有其他建议,如果我在这里做的不是最佳选择!

Are you using the Automapper Collection Extensions? 您是否正在使用Automapper集合扩展? If not, this should solve your problem: https://github.com/AutoMapper/AutoMapper.Collection 如果没有,这应该可以解决您的问题: https : //github.com/AutoMapper/AutoMapper.Collection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM