当我尝试将查询结果传递给 Dto 对象时出现异常 "Error mapping types"

Exception when I try to pass query result to Dto object "Error mapping types"

我正在尝试使用 Linq-to-SQL

对我的数据库进行基本查询

查询正确完成,我用 LinqPad 试过了,它有效,问题是(我想是的,我不是专家)试图将查询结果传递给我 DTO object DtoAsset

我用谷歌搜索了它,但我无法理解错误的原因。

AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types: EntityQueryable1 -> List1 Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1[[<>f__AnonymousType13[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.List`1[[API.Dtos.DtoAsset, API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] ---> AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types: <>f__AnonymousType13 -> DtoAsset <>f__AnonymousType13[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] -> API.Dtos.DtoAsset at lambda_method19(Closure , <>f__AnonymousType13 , DtoAsset , ResolutionContext ) at lambda_method18(Closure , Object , List1 , ResolutionContext ) --- End of inner exception stack trace ---
at lambda_method18(Closure , Object , List`1 , ResolutionContext )
at API.Controllers.InventoryController.SearhInventory() in C:\WebApp\backend\API\Controllers\InventoryControllers.cs:line 47
at lambda_method6(Closure , Object )

端点

[HttpGet("Search/")]
public async Task<ActionResult<List<DtoAsset>>> SearhInventory()
{
    var query =
        from a in context.Assets
        join i in context.Inventories
        on a.inventory_id equals i.inventory_id
        where a.inventory_id == 1
        select new { asset_id = a.asset_id, name = a.name, inventory_id = a.inventory_id };

    await query.ToListAsync();

    List<DtoAsset> dto = mapper.Map<List<DtoAsset>>(query);
    return dto;
}

映射器

namespace API.Map
{
    public class AutoMapper : Profile
    {
        public AutoMapper()
        {
            #region Inventory
            CreateMap<Inventory, DtoInventory>().ReverseMap();
            //Create
            CreateMap<DtoInventoryCreate, Inventory>();
            #endregion

            #region Asset
            CreateMap<Asset, DtoAsset>().ReverseMap();
            //Create
            CreateMap<DtoAssetCreate, Asset>();

            #endregion
        }
    }
}

型号

public class Asset
{
    public int asset_id { get; set; }
    public int code { get; set; }
    public string name { get; set; }
    public int inventory_id { get; set; }
    public Inventory Inventory { get; set; }
}

public class Inventory
{
    public int inventory_id { get; set; }
    public string name { get; set; }
    public string location { get; set; }
    public int status { get; set; }
    public DateTime? created_date { get; set; }
    public List<Asset> Assets { get; set; }

}

DTO

namespace API.Dtos
{
    public class DtoAsset
    {
        public int asset_id { get; set; }
        public int code { get; set; }
        public string name { get; set; }
        public int inventory_id { get; set; }
    }

    public class DtoInventory
    {
        public int inventory_id { get; set; }
        public string name { get; set; }
        public string location { get; set; }
        public bool status { get; set; }
        public DateTime created_date { get; set; }
        public List<Asset> Assets { get; set; }
    }
}

计划

using System.Text.Json.Serialization;
using API.Data;
using Microsoft.EntityFrameworkCore;

var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddCors(options =>
{
    options.AddPolicy(name: MyAllowSpecificOrigins,
                      policy =>
                      {
                          policy.WithOrigins("http://localhost:3000")
                            .AllowAnyMethod()
                            .AllowAnyHeader();
                      });
});
//  AutoMapper
builder.Services.AddAutoMapper(typeof(Program));

// MS SQL Connector start...
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// ...end

builder.Services.AddControllers().AddJsonOptions(
    x => x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
//      SSL Certifitate = Disable
// app.UseHttpsRedirection();

// CORS!
app.UseCors(MyAllowSpecificOrigins);
app.UseAuthorization();

app.MapControllers();

app.Run();

您的 query.ToList() 不是 return 资产列表,而是包含与资产具有相同属性的新匿名对象的列表。你想要的是:

query = ... select new Asset { asset_id = a.asset_id, name = a.name, inventory_id = a.inventory_id };

编辑:我刚刚注意到,您也调用了 toList(),但您没有在任何地方分配结果。