我正在嘗試使用 Linq-to-SQL 對我的資料庫進行基本查詢
查詢正確完成,我用 LinqPad 嘗試過,它可以作業,問題(我想是的,我不是專家)是在嘗試將查詢結果傳遞給我的DTO object DtoAsset
我用谷歌搜索了它,但我無法理解錯誤的原因。
AutoMapper.AutoMapperMappingException:錯誤映射型別。
映射型別:EntityQueryable
1 -> 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,版本=6.0.0.0,文化=中性,PublicKeyToken=7cec85d7bea7798e],[System.Int32,System.Private.CoreLib,版本=6.0.0.0,文化=中性,PublicKeyToken=7cec85d7bea7798e]],API,版本=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:缺少型別映射配置或不支持的映射。映射型別:<>f__AnonymousType1
3 -> 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 ) --- 內部例外堆疊跟蹤結束 --- 在 C:\WebApp\backend\API\ 中 API.Controllers.InventoryController.SearhInventory() 的
lambda_method18(Closure , Object , List`1 , ResolutionContext )
Controllers\InventoryControllers.cs:
lambda_method6 的第 47 行(閉包,物件)
端點
[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();

uj5u.com熱心網友回復:
您的 query.ToList() 不會回傳資產串列,而是回傳具有與資產相同屬性的新匿名物件的串列。你想要的是:
query = ... select new Asset { asset_id = a.asset_id, name = a.name, inventory_id = a.inventory_id };
編輯:我剛剛注意到,你也呼叫了 toList(),但你沒有在任何地方分配結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/471019.html
上一篇:可以安全洗掉哪些遷移檔案
