C# Net Core 3.1上的AutoMapper
在NetCore3.1应用程序中,我尝试使用AutoMapper.Extensions.Microsoft.dependency7。 在解决方案中,我有3个项目:C# Net Core 3.1上的AutoMapper,c#,asp.net-core,automapper,asp.net-core-3.1,C#,Asp.net Core,Automapper,Asp.net Core 3.1,在NetCore3.1应用程序中,我尝试使用AutoMapper.Extensions.Microsoft.dependency7。 在解决方案中,我有3个项目: 内容(启动项目) 核心 实体框架 nuget安装后,这是我的代码: 内容项目中的Startup.cs: using AutoMapper; using Project.Content.EntityFrameWork; using Project.Content.Dto; using Microsoft.AspNetCore.Bui
- 内容(启动项目)
- 核心
- 实体框架
using AutoMapper;
using Project.Content.EntityFrameWork;
using Project.Content.Dto;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Project.Content
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Auto Mapper Configurations
services.AddAutoMapper(typeof(AutoMapping));
string connectionString = Configuration["ConnectionString:Default"];
services.AddDbContext<ProjectContext>(options =>
options.UseSqlServer(connectionString));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
使用AutoMapper;
使用Project.Content.EntityFrameWork;
使用Project.Content.Dto;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.EntityFrameworkCore;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Hosting;
名称空间项目.Content
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddControllers();
//自动映射器配置
AddAutoMapper(类型(自动映射));
string connectionString=Configuration[“connectionString:Default”];
services.AddDbContext(选项=>
使用SQLServer(connectionString));
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapControllers();
});
}
}
}
内容项目中的AutoMapping.cs:
using AutoMapper;
using Project.Content.Core.Domain.Model;
namespace Project.Content.Dto
{
class AutoMapping : Profile
{
public AutoMapping()
{
CreateMap<Exercise, ExerciseDto>();
CreateMap<ExerciseDto, Exercise>();
}
}
}
使用AutoMapper;
使用Project.Content.Core.Domain.Model;
命名空间Project.Content.Dto
{
类自动映射:配置文件
{
公共自动映射()
{
CreateMap();
CreateMap();
}
}
}
这是我试图映射的控制器:
using AutoMapper;
using Project.Content.EntityFrameWork;
using Project.Content.Dto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Project.Content.Controllers
{
[ApiController]
[Route("/exercise")]
public class ExercisesController : ControllerBase
{
private readonly ILogger<ExercisesController> _logger;
private ProjectContext _dbContext;
private IMapper _mapper;
public ExercisesController(ILogger<ExercisesController> logger, ProjectContext dbContext, IMapper mapper)
{
_logger = logger;
_dbContext = dbContext;
_mapper = mapper;
}
[HttpGet("{id}")]
public ExerciseDto GetById(int id)
{
var exercise = _mapper.Map<ExerciseDto>(_dbContext.Exercises.Where(x => x.Id == id));
return exercise;
}
}
}
使用AutoMapper;
使用Project.Content.EntityFrameWork;
使用Project.Content.Dto;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Logging;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
命名空间Project.Content.Controllers
{
[ApiController]
[路线(“/练习”)]
公共类练习控制器:ControllerBase
{
专用只读ILogger\u记录器;
私有项目上下文_dbContext;
专用图像映射器;
公共练习控制器(ILogger记录器、ProjectContext dbContext、IMapper映射器)
{
_记录器=记录器;
_dbContext=dbContext;
_映射器=映射器;
}
[HttpGet(“{id}”)]
公共执行到GetById(int id)
{
var exercise=_mapper.Map(_dbContext.Exercises.Where(x=>x.Id==Id));
复训;
}
}
}
在此控制器中,当它尝试映射对象时,会显示错误:
AutoMapper.AutoMappingException:缺少类型映射配置
或不支持的映射
映射类型:EntityQueryable1->ExerciseDto
Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable
1[[Project.Content.Core.Domain.Model.Exercise,
Project.Content.Core,版本=1.0.0.0,区域性=中性,
PublicKeyToken=null]->Project.Content.Dto.ExerciseDto位于
lambda_方法(闭包,实体可查询`1,用于,
lambda_方法(闭包、对象、对象、,
ResolutionContext)在
中的Project.Content.Controllers.ExercisesController.GetById(Int32 id)
C:\Projects\Project\Project.Content\Project.Content.Service\Controllers\exercisecontroller.cs:line
44 at lambda_方法(闭包、对象、对象[])at
Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(对象
目标,对象[]参数)位于
Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper
映射器,ObjectMethodExecutor,对象控制器,对象[]
(a)在
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
在
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(状态)&
接下来,在
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
---来自引发异常的上一个位置的堆栈结束跟踪---在
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed
(上下文)在
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(状态)&
接下来,在
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
---来自引发异常的上一个位置的堆栈结束跟踪---在
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g|u waiting | 19|0(ResourceInvoker
调用程序、任务lastTask、状态next、作用域、对象状态、布尔值
完成)在
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g|u Logged|17|u 1(ResourceInvoker
调用程序)在
Microsoft.AspNetCore.Routing.EndpointMiddleware.g|u waitRequestTask | 6|0(端点
端点、任务请求任务、ILogger记录器)位于
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext
(上下文)在
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext
(上下文)
您正在尝试映射
IQueryable
,因为它使用。您需要通过u执行查询
[HttpGet("{id}")]
public ExerciseDto GetById(int id)
{
var exercises = _dbContext.Exercises.Where(x => x.Id == id).ToList();
return _mapper.Map<IEnumerable<ExerciseDto>>(exercises);
}
[HttpGet("{id}")]
public ExerciseDto GetById(int id)
{
return _mapper.ProjectTo<ExerciseDto>(_dbContext.Exercises.Where(x => x.Id == id)).ToList();
}