2017-10-14 151 views
0

我创建了新的asp mvc core 2.0角模板,并添加了我想用Automapper映射到控制器中的模型和dtos。我包含了automapper 6.1.1。当我导航到http://localhost:61031/api/classes时,它给了我一个例外,缺少类型映射配置或不支持的映射。我阅读了很多帖子,但无法找到正在发生的事情。Asp MVC core 2.0 automapper('Missing type map configuration or unsupported mapping。')

我的代码如下:项目

概述 enter image description here

模型

Models.Classes.cs

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using System.Threading.Tasks; 

namespace WebApplication1.Models 
{ 
    public class Classes 
    { 
     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int Id { get; set; } 

     [Required] 
     [MaxLength(50)] 
     public string ClassName { get; set; } 

     [Required] 
     [Range(1, 50)] 
     public int MaxStudents { get; set; } 

     public ICollection<Students> Students { get; set; } = new List<Students>(); 
    } 
} 

Models.Students.cs

using System; 

namespace WebApplication1.Models 
{ 
    public class Students 
    { 
     public int Id { get; set; } 

     public string FirstName { get; set; } 

     public string LastName { get; set; } 

     public DateTime DateOfBirth { get; set; } 

     public Gender Gender { get; set; } 



     public Classes Classes { get; set; } 

     public int ClassId { get; set; } 
    } 
} 

Models.EdulyContext.cs

using Microsoft.EntityFrameworkCore; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 

namespace WebApplication1.Models 
{ 
    public class EdulyContext : DbContext 
    { 
     public EdulyContext(DbContextOptions<EdulyContext> options) : base(options) 
     { 
      Database.Migrate(); 
     } 

     public DbSet<Classes> Classes { get; set; } 
     public DbSet<Students> Students { get; set; } 
    } 
} 

DTOS

Dto.ClassesDtos.ClassesDto.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using WebApplication1.Models; 

namespace WebApplication1.Dto.ClassesDtos 
{ 
    public class ClassesDto 
    { 
     public string ClassName { get; set; } 

     public int MaxStudents { get; set; } 

     public ICollection<Students> Students { get; set; } = new List<Students>(); 
    } 
} 

Dto.StudentsDtos.StudentDto.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using WebApplication1.Models; 

namespace WebApplication1.Dto.StudentsDtos 
{ 
    public class StudentsDto 
    { 
     public int Id { get; set; } 

     public string FirstName { get; set; } 

     public string LastName { get; set; } 

     public DateTime DateOfBirth { get; set; } 

     public Gender Gender { get; set; } 



     public Models.Classes Classes { get; set; } 

     public int ClassId { get; set; } 
    } 
} 

CONTROLLERS

Controllers.ClassesController.cs

using AutoMapper; 
using Microsoft.AspNetCore.Mvc; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using WebApplication1.Dto.ClassesDtos; 
using WebApplication1.Repositories.Interfaces; 

namespace WebApplication1.Controllers 
{ 
    [Route("api/classes")] 
    public class ClassesController : Controller 
    { 
     private IClassesRepository _classesRepository; 

     public ClassesController(IClassesRepository classesRepository) 
     { 
      _classesRepository = classesRepository; 
     } 

     [HttpGet()] 
     public IActionResult GetClasses() 
     { 
      var classesEntities = _classesRepository.GetClasses(); 
      var results = Mapper.Map<ClassesDto>(classesEntities); 

      return Ok(results); 

     } 

    } 
} 

的Program.cs

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.AspNetCore; 
using Microsoft.AspNetCore.Hosting; 
using Microsoft.Extensions.Configuration; 
using Microsoft.Extensions.Logging; 

namespace WebApplication1 
{ 
    public class Program 
    { 
     public static void Main(string[] args) 
     { 
      var host = BuildWebHost(args); 
      host.Run(); 
     } 

     public static IWebHost BuildWebHost(string[] args) 
     { 
      return new WebHostBuilder() 
       .UseKestrel() 
       .UseContentRoot(Directory.GetCurrentDirectory()) 
       .ConfigureAppConfiguration((builderContext, config) => 
       { 
        IHostingEnvironment env = builderContext.HostingEnvironment; 
       }) 
       .UseIISIntegration() 
       .UseStartup<Startup>() 
       .Build(); 
     } 
    } 
} 

Startup.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.AspNetCore.Builder; 
using Microsoft.AspNetCore.Hosting; 
using Microsoft.AspNetCore.SpaServices.Webpack; 
using Microsoft.Extensions.Configuration; 
using Microsoft.Extensions.DependencyInjection; 
using WebApplication1.Models; 
using WebApplication1.Dto; 
using Microsoft.EntityFrameworkCore; 
using WebApplication1.Repositories.Interfaces; 
using WebApplication1.Repositories; 

namespace WebApplication1 
{ 
    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.AddMvc(); 

      var connectionString = @"server=(localdb)\mssqllocaldb;Database=EdulyDbCore2;Trusted_Connection=True"; 
      services.AddDbContext<EdulyContext>(o => o.UseSqlServer(connectionString)); 

      services.AddScoped<IClassesRepository, ClassesRepository>(); 
     } 

     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
     public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
     { 
      if (env.IsDevelopment()) 
      { 
       app.UseDeveloperExceptionPage(); 
       app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions 
       { 
        HotModuleReplacement = true 
       }); 
      } 
      else 
      { 
       app.UseExceptionHandler("/Home/Error"); 
      } 


      app.UseStaticFiles(); 


      AutoMapper.Mapper.Initialize(cfg => 
      { 
       cfg.CreateMap<Models.Classes, Dto.ClassesDtos.ClassesDto>(); 
       cfg.CreateMap<Dto.ClassesDtos.ClassesDto, Models.Classes>(); 

       cfg.CreateMap<Models.Students, Dto.StudentsDtos.StudentsDto>(); 
       cfg.CreateMap<Dto.StudentsDtos.StudentsDto, Models.Students>(); 
      }); 

      app.UseMvc(routes => 
      { 
       routes.MapRoute(
        name: "default", 
        template: "{controller=Home}/{action=Index}/{id?}"); 

       routes.MapSpaFallbackRoute(
        name: "spa-fallback", 
        defaults: new { controller = "Home", action = "Index" }); 
      }); 
     } 
    } 
} 

在启动我映射从类classesDto两个方向只是为了某些。如果任何人有任何想法我做错了,帮助将被大大提升。干杯!

回答

0

经过一个小休息后,我突然意识到自己的错误。 在类控制器中,获取返回类型应该是IEnumerable

因此: var results = Mapper.Map(classesEntities);

应该变成: var results = Mapper.Map>(classesEntities);