Automapper flatten from three levels to two levels - automapper

I use automapper v. 9 and I need to transform a 3 levels structure to a 2 levels structure.
I already tried with the IncludeMembers option, but I can't achieve the result I want.
Here is my model classes (simplified)
public partial class Mail
{
public Mail()
{
Mail_Files = new HashSet<Mail_Files>();
}
[Key]
public int Id { get; set; }
public string Oggetto { get; set; }
public string Testo { get; set; }
[InverseProperty("IdMailNavigation")]
public virtual ICollection<Mail_Files> Mail_Files { get; set; }
}
public partial class Mail_Files
{
[Key]
public int Id { get; set; }
public int IdMail { get; set; }
public int IdFile { get; set; }
[ForeignKey(nameof(IdFile))]
[InverseProperty(nameof(Files.Mail_Files))]
public virtual Files IdFileNavigation { get; set; }
[ForeignKey(nameof(IdMail))]
[InverseProperty(nameof(Mail.Mail_Files))]
public virtual Mail IdMailNavigation { get; set; }
}
public partial class Files
{
public Files()
{
Mail_Files = new HashSet<Mail_Files>();
}
[Key]
public int Id { get; set; }
public int IdTipoFile { get; set; }
public string FileName { get; set; }
public int? FileSize { get; set; }
[InverseProperty("IdFileNavigation")]
public virtual ICollection<Mail_Files> Mail_Files { get; set; }
}
I have to flatten this object to this:
public class MailFileResource
{
public int Id { get; set; }
public string FileName { get; set; }
}
public class MailResource
{
public int Id { get; set; }
public string Testo { get; set; }
public IEnumerable<MailFileResource> Files { get; set; }
}

I do it this way:
CreateMap<Files, MailFileResource>()
.ForMember(d => d.FileName, opt => opt.MapFrom(s => s.FileName))
.ForMember(d => d.Id, opt => opt.MapFrom(s => s.Id));
CreateMap<Mail_Files, MailFileResource>().IncludeMembers(s => s.IdFileNavigation);
CreateMap<Mail, MailResource>()
.ForMember(d => d.Files , opt => opt.MapFrom(s => s.Mail_Files))
.ForMember(d => d.Protezione, opt => opt.MapFrom(s => s.Protezione));
My mistake was to use IncludeMembers in Mail -> MailResource mapping too

Related

EF Core with AutoMapper

I have a DTO containing properties and a Model, eg student can have more than one module and a module can be associated with more than one student. the properties are mapping fine but the Model doesn't map.
public class GetStudentByIdMapping : Profile
{
public GetStudentByIdMapping()
{
CreateMap<Student,StudentDetails>();
CreateMap<Module, StudentDetails>()
.ForPath(dest => dest.StudentModules.ModuleName, opt => opt.MapFrom(m => m.ModuleName))
.ForPath(dest => dest.StudentModules.ModuleCode, opt => opt.MapFrom(m => m.ModuleCode))
.ForPath(dest => dest.StudentModules.Description, opt => opt.MapFrom(m => m.Description))
.ReverseMap();
}
}
public async Task<StudentDetails> GetStudent(int studentId)
{
var student = context.Student
.Where(s => s.StudentId == studentId)
.FirstOrDefault();
var module = await context.Order
.Include(m => m.Module)
.Where(o => o.StudentId == studentId)
.Select(m => m.Module).ToListAsync();
var studMap = Mapper.Map<StudentDetails>(student);
Mapper.Map<StudentDetails>(module);
return studMap;
}
These are the ViewModels I want to map to the Models Model in the StudentDetails ViewModel
public class StudentDetails
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public StudentModule StudentModules { get; set; }
}
public class StudentModule
{
public string ModuleName { get; set; }
public string ModuleCode { get; set; }
public string Description { get; set; }
}
These are my Entities generated by EF Core
public partial class Student
{
public Student()
{
Order = new HashSet<Order>();
}
public int StudentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public virtual ICollection<Order> Order { get; set; }
}
public partial class Module
{
public Module()
{
Order = new HashSet<Order>();
}
public int ModuleId { get; set; }
public int? LectureId { get; set; }
public string ModuleName { get; set; }
public string ModuleCode { get; set; }
public string Description { get; set; }
public string ModulePath { get; set; }
public virtual Lecture Lecture { get; set; }
public virtual ICollection<Order> Order { get; set; }
}
You only need to pass the created destination to the second map call:
Just try the following code when mapping:
var studMap = Mapper.Map<Student,StudentDetails>(student);
Mapper.Map<Module,StudentDetails>(module,studMap);
Then the studMap will receive all mapped fields value.

How can I Automap a missmatch between two models with a different number of fields?

I use Visual Studio 2019 and .net core 3.1. I have a couple of related models, CustomerCountries and CustomerRegions in a one-to-many relationship, i.e. 1 CustomerCountries has many CustomersRegions.
In the views of the regions I want to show the name of the countries, not the id, that's why I created a ViewModel:
public class CustomerCountryRegionVM
{
public int IdCustomerRegion { get; set; }
public string CustomerRegion { get; set; }
public int IdCustomerCountry { get; set; }
public string CustomerCountry { get; set; }
}
The issue is that the regions model does not have the CustomerCountry field, it has the foreign primary key IdCustomerCountry. So I don't know how to do the mapping, I've tried it but I get a missmatch. One has 4 fields and the other 3. Do I have to make a discount for the viewmodel? or is there another option?
public class CustomerRegionsDto
{
public int IdCustomerRegion { get; set; }
public string CustomerRegion { get; set; }
[ForeignKey("CustomerCountryId")]
public int IdCustomerCountry { get; set; }
public CustomerCountriesDto CustomerCountryDto { get; set; }
}
public class CustomerRegions
{
[Key]
public int IdCustomerRegion { get; set; }
[StringLength(50, ErrorMessage = "Longitud máxima para la región: 50")]
public string CustomerRegion { get; set; }
[ForeignKey("IdCustomerCountry")]
public int IdCustomerCountry { get; set; }
public CustomerCountries CustomerCountry { get; set; }
}
Automapping class
CreateMap<CustomerRegionsDto, CustomerRegions>();
CreateMap<CustomerRegions, CustomerRegionsDto>();
CreateMap<CustomerCountryRegionVM, CustomerRegions>();
CreateMap<CustomerRegions, CustomerCountryRegionVM>();
CreateMap<CustomerCountryRegionVM, CustomerCountryRegionDto>();
CreateMap<CustomerCountryRegionDto, CustomerCountryRegionVM>();
In the controller:
var model = _mapper.Map<CustomerCountryRegionVM, CustomerRegionsDto>(ccr);
The other model:
public class CustomerCountries
{
public int IdCustomerCountry { get; set; }
[StringLength(50, ErrorMessage = "Longitud máxima para el país: 50")]
public string CustomerCountry { get; set; }
public ICollection<CustomerRegions> CustomerRegions { get; set; }
}
You can map particular property
CreateMap<BusinessService.Model.PermitDashboard, BusinessService.Model.Permit>()
.ForMember(x => x.PermitId, opt => opt.MapFrom(z => z.PermitId))
.ForMember(x => x.PermitTitle, opt => opt.MapFrom(z => z.PermitTitle))
.ForPath(x => x.PermitType.PermitTypeName, opt => opt.MapFrom(z => z.PermitType))
.ForMember(x => x.EffectiveDt, opt => opt.MapFrom(z => z.EffectiveDt))
.ForMember(x => x.ExpirationDt, opt => opt.MapFrom(z => z.ExpirationDt))
.ReverseMap();

How to map related data with AutoMapper

I'm trying to map three entity models into one viewmodel using AutoMapper. The final output should be a recursive category tree with products in the categories. The category tree is working, but the Products-property of the viewmodel is null. My query is returning categories and products, so I'm thinking that the mapping doesn't know how to map the products to the viewmodel.
My entity models:
public class ProductCategory
{
public int Id { get; set; }
public int SortOrder { get; set; }
public string Title { get; set; }
[ForeignKey(nameof(ParentCategory))]
public int? ParentId { get; set; }
// Nav.props:
public ProductCategory ParentCategory { get; set; }
public ICollection<ProductCategory> Children { get; set; }
public List<ProductInCategory> ProductInCategory { get; set; }
}
public class ProductInCategory
{
public int Id { get; set; }
public int ProductId { get; set; }
public int SortOrder { get; set; }
public int ProductCategoryId { get; set; }
public bool IsProductCategoryFrontPage { get; set; }
// Nav.props.
public Product Product { get; set; }
public ProductCategory ProductCategory { get; set; }
}
public class Product
{
public int Id { get; set; }
public string Title { get; set; }
public string Info { get; set; }
public decimal Price { get; set; }
// Nav.prop:
public List<ProductInCategory> InCategories { get; set; }
}
My viewmodel:
public class ViewModelProductCategory
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Title { get; set; }
public int SortOrder { get; set; }
public string ProductCountInfo
{
get
{
return Products != null && Products.Any() ? Products.Count().ToString() : "0";
}
}
public ViewModelProductCategory ParentCategory { get; set; }
public IEnumerable<ViewModelProductCategory> Children { get; set; }
public IEnumerable<ViewModelProduct> Products { get; set; }
}
I have tried this mapping (maps categories, but no products):
CreateMap<ProductCategory, ViewModelProductCategory>();
CreateMap<ViewModelProductCategory, ProductCategory>();
I have tried this mapping (maps categories, but no products):
CreateMap<ProductCategory, ViewModelProductCategory>()
.ForMember(dto => dto.Id, opt => opt.MapFrom(src => src.Id))
.ForMember(dto => dto.ParentId, opt => opt.MapFrom(src => src.ParentId))
.ForMember(dto => dto.Title, opt => opt.MapFrom(src => src.Title))
.ForMember(dto => dto.SortOrder, opt => opt.MapFrom(src => src.SortOrder))
.ForMember(dto => dto.Children, opt => opt.MapFrom(src => src.Children))
.ForMember(dto => dto.Products, opt => opt.MapFrom(src => src.ProductInCategory));
Looks like you want to skip the junction (link) entity in the view model.
To do that, first create a mapping from Product to ViewModelProduct and then define a mapping from ProductCategory.ProductInCategory to ViewModelProductCategory.Products using projection converting List<ProductInCategory> to IEnumerable<Product>. AutoMapper will take care converting IEnumerable<Product> to List<ViewModelProduct> the same way as if you were querying Products:
CreateMap<Product, ViewModelProduct>();
CreateMap<ProductCategory, ViewModelProductCategory>()
.ForMember(dst => dst.Products, opt => opt.MapFrom(
src => src.ProductInCategory.Select(pc => pc.Product)));

Automapper to map child tables

I have 3 tables in my database and these are the classes:
public class PRAT
{
public PRAT()
{
PRAT_RIC = new HashSet<PRAT_RIC>();
}
public int ID { get; set; }
public string PRATICA { get; set; }
public int ANNO { get; set; }
public string VARIANTE { get; set; }
[...] other fields
}
public class PRAT_RIC
{
public int ID { get; set; }
public int IDPRAT { get; set; }
public int IDANAG { get; set; }
public int? IDTITRIC { get; set; }
public virtual ANAGRAFI IDANAGNavigation { get; set; }
}
public class ANAGRAFI
{
public int ID { get; set; }
public string TIPO { get; set; }
public string TITOLO { get; set; }
public string COGNOME { get; set; }
public string NOME { get; set; }
public string CODFISC { get; set; }
}
So, the relations are:
Prat -> Prat_Ric (Prat.Id=Prat_Ric.IdPrat) -> Anagrafi (Anagrafi.Id=Prat_Ric.IdAnag)
These are the destination classes:
public class PraticaResource
{
public int ID { get; set; }
public string PRATICA { get; set; }
public int ANNO { get; set; }
public string VARIANTE { get; set; }
public ICollection<RichiedentiResource> RICHIEDENTI { get; set; }
}
public class RichiedentiResource
{
public int ID { get; set; }
public int IDANAG { get; set; }
public string TITOLO { get; set; }
public string COGNOME { get; set; }
public string NOME { get; set; }
public string CODFISC { get; set; }
public string TITRIC { get; set; }
}
I created the map with
CreateMap<PRAT_RIC, RichiedentiResource>()
.ForMember(p => p.COGNOME, opt => opt.MapFrom(ti => ti.IDANAGNavigation.COGNOME))
.ForMember(p => p.NOME, opt => opt.MapFrom(ti => ti.IDANAGNavigation.NOME));
Is it possible to map all the properties of RichiedentiResource to the ANAGRAFI fields with the same name so that i haven't to write every single field mapping?
Thank you.
If you don't need anything from the navigation class, then you can just skip it when mapping at the top level:
CreateMap<PRAT, PraticaResource>()
.ForMember(p => p.PRAT_RIC, opt => opt.MapFrom(src => src.PRAT_RIC.Select(x => x.IDANAGNavigation))
Then you just need the mapping from ANAGRAFI -> RichiedentiResource, which should work out the field mappings automatically.

AutoMapper 6 - How can I create a mapping that ignore and map a list of object

I'm a little newbie on AutoMapper, I don't find almost nothing about v6.0 on Stackoverflow and Github. I need help on this problem
I have this two Entities:
public class DocFinanceiro
{
public int AutoId { get; set; }
public virtual ICollection<QuitacaoDocFinan> QuitacoesDocFinan { get; set; }
}
public class QuitacaoDocFinan
{
public int AutoId { get; set; }
public int DocFinanceiroId { get; set; }
public virtual DocFinanceiro DocFinanceiro { get; set; }
public decimal ValorTotal { get; set; }
}
}
And his ViewModels:
public class DocFinanceiroViewModel
{
public DocFinanceiroViewModel()
{
ValorPago = QuitacoesDocFinan.Where(x => x.Cancelada == false).Sum(x => x.ValorTotal);
}
public virtual ICollection<QuitacaoDocFinanViewModel> QuitacoesDocFinan { get; set; }
public decimal ValorPago { get; set; }
}
public class QuitacaoDocFinanViewModel
{
public int AutoId { get; set; }
public int DocFinanceiroId { get; set; }
public virtual DocFinanceiroViewModel DocFinanceiro { get; set; }
public decimal ValorTotal { get; set; }
}
And mapping between DocFinanceiro and DocFinanceiroViewModel:
public class DomainToViewModelMappingProfile : Profile
{
public DomainToViewModelMappingProfile()
{
CreateMap<DocFinanceiro, DocFinanceiroViewModel>().ForMember(x => x.ValorPago, y => y.Ignore())
.MaxDepth(3)
.PreserveReferences();
CreateMap<QuitacaoDocFinan, QuitacaoDocFinanViewModel>();
}
}
This mapping works when I set only one of these property
.ForMember(x => x.ValorPago, y => y.Ignore())
or
.MaxDepth(1).PreserveReferences();
, but when I try two cause an exception. I search on everywhere, but no success.
And controller where that I make the mapping:
var documentos = Mapper.Map<IEnumerable<DocFinanceiro>, IEnumerable<DocFinanceiroViewModel>>(*repository*);
Sorry if make some mistake, but I don't what to do...
You are trying to access QuitacoesDocFinan collection in DocFinanceiroViewModel constructor before initializing. Your DocFinanceiroViewModel should be something like this:
public class DocFinanceiroViewModel
{
public virtual ICollection<QuitacaoDocFinanViewModel> QuitacoesDocFinan { get; set; }
public decimal ValorPago
{
get
{
return QuitacoesDocFinan.Where(x => x.Cancelada == false).Sum(x => x.ValorTotal);
}
}
}

Resources