在自动映射器中将一种类型的通用 class 转换为另一种类型
Generic class of one type to another type in auto mapper
我有这个通用分页 class:我想映射 PagedList<Caste> to PagedList<CasteModel>
public class PagedList<T>
{
public PagedList()
{
}
public PagedList(IList<T> source, int pageNumber, int pageSize)
{
this.TotalItems = source.Count;
this.PageNumber = pageNumber;
this.PageSize = pageSize;
this.Items = source;
}
public int TotalItems { get; set; }
public int PageNumber { get; set; }
public int PageSize { get; set; }
public IEnumerable<T> Items { get; set; }
public int TotalPages => (int)Math.Ceiling(this.TotalItems / (double)this.PageSize);
}
以及模型和视图模型类
public class Caste
{
public int Id { get; set; }
public string CasteCode { get; set; }
public string CasteDesc { get; set; }
public bool IsActive { get; set; }
public int? CasteParentId { get; set; }
public virtual Caste CasteParent { get; set; }
public virtual ICollection<Caste> CasteChildren { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
}
public class CasteModel
{
public int Id { get; set; }
public string CasteCode { get; set; }
public string CasteDesc { get; set; }
public bool IsActive { get; set; }
public int? CasteParentId { get; set; }
}
下面是我的自动映射器配置
public class AppProfile : Profile
{
public AppProfile()
{
//Masters
CreateMap<CasteModel, Caste>();
CreateMap<Caste, CasteModel>();
CreateMap(typeof(PagedList<>), typeof(PagedList<>));
// CreateMap<PagedList<Caste>, PagedList<CasteModel>>(); ---This also checked
}
这是控制器中映射的代码
PagedList<Caste> result = new PagedList<Caste>
{
Items = new List<Caste> { new Caste { Id = 7, CasteCode="" } },
TotalItems = 1
};
var pagedListOfDtos = Mapper.Map<PagedList<CasteModel>>(result);
当执行下面的错误时,我得到下面的异常
"Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance."
我正在使用 Asp.net 核心和 automapper 6.1。代码是根据下面link编写的
请建议我的解决方案尝试了很多都得到相同的消息
对于 Mapper.Map<PagedList<CasteModel>>(result);
,您需要像下面 Startup.cs
中那样初始化 Mapper
public void ConfigureServices(IServiceCollection services)
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<AppProfile>();
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
但是,建议使用Dependence Injection
来解析Mapper
。
安装包AutoMapper.Extensions.Microsoft.DependencyInjection
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(Startup));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
用例
public class ValuesController : ControllerBase
{
private readonly IMapper _mapper;
public ValuesController(IMapper mapper)
{
_mapper = mapper;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
PagedList<Caste> result = new PagedList<Caste>
{
Items = new List<Caste> { new Caste { Id = 7, CasteCode = "" } },
TotalItems = 1
};
var pagedListOfDtos = _mapper.Map<PagedList<CasteModel>>(result);
return new string[] { "value1", "value2" };
}
}
我有这个通用分页 class:我想映射 PagedList<Caste> to PagedList<CasteModel>
public class PagedList<T>
{
public PagedList()
{
}
public PagedList(IList<T> source, int pageNumber, int pageSize)
{
this.TotalItems = source.Count;
this.PageNumber = pageNumber;
this.PageSize = pageSize;
this.Items = source;
}
public int TotalItems { get; set; }
public int PageNumber { get; set; }
public int PageSize { get; set; }
public IEnumerable<T> Items { get; set; }
public int TotalPages => (int)Math.Ceiling(this.TotalItems / (double)this.PageSize);
}
以及模型和视图模型类
public class Caste
{
public int Id { get; set; }
public string CasteCode { get; set; }
public string CasteDesc { get; set; }
public bool IsActive { get; set; }
public int? CasteParentId { get; set; }
public virtual Caste CasteParent { get; set; }
public virtual ICollection<Caste> CasteChildren { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
}
public class CasteModel
{
public int Id { get; set; }
public string CasteCode { get; set; }
public string CasteDesc { get; set; }
public bool IsActive { get; set; }
public int? CasteParentId { get; set; }
}
下面是我的自动映射器配置
public class AppProfile : Profile
{
public AppProfile()
{
//Masters
CreateMap<CasteModel, Caste>();
CreateMap<Caste, CasteModel>();
CreateMap(typeof(PagedList<>), typeof(PagedList<>));
// CreateMap<PagedList<Caste>, PagedList<CasteModel>>(); ---This also checked
}
这是控制器中映射的代码
PagedList<Caste> result = new PagedList<Caste>
{
Items = new List<Caste> { new Caste { Id = 7, CasteCode="" } },
TotalItems = 1
};
var pagedListOfDtos = Mapper.Map<PagedList<CasteModel>>(result);
当执行下面的错误时,我得到下面的异常
"Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance."
我正在使用 Asp.net 核心和 automapper 6.1。代码是根据下面link编写的
对于 Mapper.Map<PagedList<CasteModel>>(result);
,您需要像下面 Startup.cs
Mapper
public void ConfigureServices(IServiceCollection services)
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<AppProfile>();
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
但是,建议使用Dependence Injection
来解析Mapper
。
安装包
AutoMapper.Extensions.Microsoft.DependencyInjection
Startup.cs
public void ConfigureServices(IServiceCollection services) { services.AddAutoMapper(typeof(Startup)); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }
用例
public class ValuesController : ControllerBase { private readonly IMapper _mapper; public ValuesController(IMapper mapper) { _mapper = mapper; } // GET api/values [HttpGet] public ActionResult<IEnumerable<string>> Get() { PagedList<Caste> result = new PagedList<Caste> { Items = new List<Caste> { new Caste { Id = 7, CasteCode = "" } }, TotalItems = 1 }; var pagedListOfDtos = _mapper.Map<PagedList<CasteModel>>(result); return new string[] { "value1", "value2" }; } }