C# 在.NET中将OData的IQueryable映射到域模型

C# 在.NET中将OData的IQueryable映射到域模型,c#,.net,odata,automapper,iqueryable,C#,.net,Odata,Automapper,Iqueryable,我最近在ASP.NET核心web API中实现了OData。只要我直接返回数据库模型,我就成功了。然而,当我试图返回域模型时,我就遇到了麻烦 基本问题涉及将数据类映射到域类,同时维护IQueryable返回类型。虽然我发现使用AutoMapper的MapTo扩展方法取得了部分成功,但我发现使用$extend方法扩展也是域对象的实体集合时失败 我创建了一个示例项目来说明这个问题。您可以在github上查看或下载完整的项目。请参见下面的描述 给定以下两个数据库类: public class Prod

我最近在ASP.NET核心web API中实现了OData。只要我直接返回数据库模型,我就成功了。然而,当我试图返回域模型时,我就遇到了麻烦

基本问题涉及将数据类映射到域类,同时维护IQueryable返回类型。虽然我发现使用AutoMapper的MapTo扩展方法取得了部分成功,但我发现使用$extend方法扩展也是域对象的实体集合时失败

我创建了一个示例项目来说明这个问题。您可以在github上查看或下载完整的项目。请参见下面的描述

给定以下两个数据库类:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public ICollection<Order> Orders { get; set; }

    public Product() {
        Orders = new Collection<Order>();
    }
}

public class Order
{
    public int Id { get; set; }
    public Double Price { get; set; }  
    public DateTime OrderDate { get; set; }

    [Required]
    public int ProductId { get; set; }
    public Product Product { get; set; }    
}
最后,这里是对映射配置文件的参考:

    public static class MappingProfile
{
    public static void RegisterMappings() {
        Mapper.Initialize(cfg =>
        {
           cfg.CreateMap<Order, OrderEntity>();
           cfg.CreateMap<Product, ProductEntity>();
        });
    }
}
公共静态类映射配置文件
{
公共静态无效注册表映射(){
Mapper.Initialize(cfg=>
{
CreateMap();
CreateMap();
});
}
}
我可以通过简单地返回一个列表而不是控制器中的IEnumerable来解决这个问题,但是这当然会触发对数据库的大型查询,这将是性能密集型的


如上所述,您可以在Github上找到完整项目的链接。如果你找到任何答案,请告诉我

我只做了一些小的修改就可以让它正常工作

更新域模型:

public class ProductEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public ICollection<Order> Orders { get; set; }
}

public class OrderEntity
{
    public int Id { get; set; }
    public double Price { get; set; }
    public DateTime OrderDate { get; set; }

    [Required]
    public int ProductId { get; set; }
    public Product Product { get; set; }
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, SalesModelBuilder modelBuilder)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseMvc(routeBuilder =>
        {
            routeBuilder.Expand().Select();
            routeBuilder.MapODataServiceRoute("ODataRoutes", "odata",
                    modelBuilder.GetEdmModel(app.ApplicationServices));
        });
}
使用以下查询:


  • OrderEntity
    是否应该有
    ProductEntity
    而不是
    Product
    属性?
        public static class MappingProfile
    {
        public static void RegisterMappings() {
            Mapper.Initialize(cfg =>
            {
               cfg.CreateMap<Order, OrderEntity>();
               cfg.CreateMap<Product, ProductEntity>();
            });
        }
    }
    
    public class ProductEntity
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    
        public ICollection<Order> Orders { get; set; }
    }
    
    public class OrderEntity
    {
        public int Id { get; set; }
        public double Price { get; set; }
        public DateTime OrderDate { get; set; }
    
        [Required]
        public int ProductId { get; set; }
        public Product Product { get; set; }
    }
    
    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, SalesModelBuilder modelBuilder)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
    
        app.UseMvc(routeBuilder =>
            {
                routeBuilder.Expand().Select();
                routeBuilder.MapODataServiceRoute("ODataRoutes", "odata",
                        modelBuilder.GetEdmModel(app.ApplicationServices));
            });
    }