Currently I have the following Objects that I need to map
OBJECT 1
public class ContactInfo
{
public int ContactInfoId { get; set; }
public ICollection<PhoneToCustomer> Phones { get; set; }
}
public class PhoneToCustomer
{
public int ContactInformationId { get; set; }
public Phone Phone { get; set; }
public int PhoneId { get; set; }
}
public class Phone
{
public int PhoneId { get; set; }
public long Number { get; set; }
public PhoneType Type { get; set; }
public string Extension { get; set; }
public string CountryCode { get; set; }
public PhoneRestrictions Restrictions { get; set; }
public bool Primary { get; set; }
}
I am trying to map it to the following object
OBJECT 2
public class ContactInfoModel
{
public int ContactInfoId { get; set; }
public ICollection<PhoneModel> Phones { get; set; }
}
public class PhoneModel
{
public int PhoneId { get; set; }
public long Number { get; set; }
public PhoneType Type { get; set; }
public string Extension { get; set; }
public string CountryCode { get; set; }
public PhoneRestrictions Restrictions { get; set; }
public bool Primary { get; set; }
}
Essentially I am wanting to eliminate the PhoneToCustomer object when mapping it. I need the ContactInfoModel.Phones to map to ContactInfo.PhoneToCustomer.Phone (to list)
In order to map ContactInfo to ContactInfoModel you need to add the following mappings:
AutoMapper.Mapper.CreateMap<Phone, PhoneModel>();
AutoMapper.Mapper.CreateMap<ContactInfo, ContactInfoModel>()
.ForMember(x => x.Phones, y => y.MapFrom(z => z.Phones.Select(q => q.Phone)));
If you want to map vice versa from ContactInfoModel to ContactInfo you can use the following mappings:
AutoMapper.Mapper.CreateMap<PhoneModel, Phone>();
AutoMapper.Mapper.CreateMap<PhoneModel, PhoneToCustomer>()
.ForMember(x => x.Phone, y => y.MapFrom(z => z))
.ForMember(x => x.ContactInformationId, y => y.Ignore())
.ForMember(x => x.PhoneId, y => y.Ignore());
AutoMapper.Mapper.CreateMap<ContactInfoModel, ContactInfo>();
Here is a test example of usage:
var contactInfo = new ContactInfo()
{
ContactInfoId = 1,
Phones = new List<PhoneToCustomer>()
{
new PhoneToCustomer()
{
Phone = new Phone(){CountryCode = "Code1", Extension = "Extension1"},
},
new PhoneToCustomer()
{
Phone = new Phone(){CountryCode = "Code2", Extension = "Extension2"}
}
}
};
var contactInfoModel = AutoMapper.Mapper.Map<ContactInfoModel>(contactInfo);
var contactInfoBack = AutoMapper.Mapper.Map<ContactInfo>(contactInfoModel);
Related
I have 2 objects : Parent and ParentDTO :
class ParentDTO
{
public string AttA { get; set; }
public List<List<BDto>> AttrBNestedList { get; set; }
}
class BDto
{
public string AttrC { get; set; }
public int AtterD { get; set; }
public string AtterE { get; set; }
public CDto AttrFobj { get; set; }
}
class CDto
{
public string AttrG { get; set; }
public int AtterH { get; set; }
}
//--------------------------------------
class Parent
{
public string AttA { get; set; }
public List<B> AttrBList { get; set; }
}
class B
{
public string AttrC { get; set; }
public int AtterD { get; set; }
public string AtterE { get; set; }
public C AttrFobj { get; set; }
public C AttrGobj { get; set; }
public C AttrHobj { get; set; }
}
class C
{
public string AttrI { get; set; }
public string AttrJ { get; set; }
public int AtterK { get; set; }
}
I want to using AutoMapper to map data from ParentDto object to Parent object
(all data in ParentDto has to transfer to Parent, but first element from AttrBNestedList copy to AttrBList)
Mapper.CreateMap<CDto, C>();
Mapper.CreateMap<BDto, B>();
Mapper.CreateMap<ParentDto, Parent>()
.ForMember(dest => dest.AttrBList , opt => opt.AttrBNestedList.singleorDefault??);
I need just singleorDefault item from List<List<BDto>> AttrBNestedList map to List<B> AttrBList
Can anyone help me. Thanks
There is a syntax error which doesn't allow you to use SingleOrDefault(), you need to use MapFrom method:
Mapper.CreateMap<ParentDTO, Parent>().ForMember(dest => dest.AttrBList, opt => opt.MapFrom(src => src.AttrBNestedList.SingleOrDefault()));
When attempting a straight forward projection using Entity Framework Core and Linq, I am getting an "Argument types do not match" exception.
I have looked into possible causes and have narrowed it down to the Select that is causing the error (see below). There is a GitHub issue describing a similar situation with simple types and optional navigation entities, but none of the suggested solutions have worked for me. It is not a nullable type and I have tried casting or using Value on any child properties. I have also tried setting the relationship to required in the DbContext which isn't exactly ideal.
Here is the Linq query in the repository:
return await _dashboardContext.PresetDashboardConfig
.Where(config => config.DashboardTypeId == dashboardType && config.OrganisationType = organisationType)
.GroupBy(config => config.GroupId)
.Select(config => new DashboardConfigDTO
{
DashboardType = config.First().DashboardTypeId,
OrganisationId = organisationId,
WidgetGroups = config.Select(group => new WidgetGroupDTO
{
Id = group.Id,
Name = group.GroupName,
TabOrder = group.TabOrder,
// Problem Select below:
Widgets = group.Widgets.Select(widget => new WidgetConfigDTO
{
IndicatorId = widget.IndicatorId,
ScopeId = widget.ScopeId.ToString(),
ParentScopeId = widget.ParentScopeId.ToString(),
WidgetType = widget.WidgetType,
WidgetSize = widget.WidgetSize,
Order = widget.Order
})
})
})
.SingleOrDefaultAsync();
And the entities:
public class DashboardConfig
{
public int Id { get; set; }
public int DashboardTypeId { get; set; }
public int OrganisationType {get; set; }
public int GroupId { get; set; }
public string GroupName { get; set; }
public int TabOrder { get; set; }
}
public class PresetDashboardConfig : DashboardConfig
{
public ICollection<PresetWidgetConfig> Widgets { get; set; }
}
public class WidgetConfig
{
public int Id { get; set; }
public int IndicatorId { get; set; }
public long ScopeId { get; set; }
public long? ParentScopeId { get; set; }
public int WidgetType { get; set; }
public int WidgetSize { get; set; }
public int Order { get; set; }
}
public class PresetWidgetConfig : WidgetConfig
{
public int PresetDashboardConfigId { get; set; }
}
And finally, the DbContext ModelBuilder:
modelBuilder.Entity<PresetDashboardConfig>(entity =>
{
entity.Property(e => e.GroupName)
.HasMaxLength(32)
.IsUnicode(false);
entity.HasMany(e => e.Widgets)
.WithOne();
});
Below are the DTO classes as per Henk's comment:
public class DashboardConfigDTO
{
public int DashboardType { get; set; }
public int OrganisationId { get; set; }
public IEnumerable<WidgetGroupDTO> WidgetGroups { get; set; }
}
public class WidgetGroupDTO
{
public int Id { get; set; }
public string Name { get; set; }
public int TabOrder { get; set; }
public IEnumerable<WidgetConfigDTO> Widgets { get; set; }
}
public class WidgetConfigDTO
{
public int IndicatorId { get; set; }
public string ScopeId { get; set; }
public string ParentScopeId { get; set; }
public int WidgetType { get; set; }
public int WidgetSize { get; set; }
public int Order { get; set; }
}
I hav been using automapper for sometime trying figure out how to handle different situation. I came across below situation and need some help figuring out the best approach. Below are my EF related classes;
public sealed class Invoice
{
public int InvoiceID { get; set; }
public DateTime InvoiceDate { get; set; }
public string CustomerName { get; set; }
public string CustomerAddress { get; set; }
public double? DiscountAmt { get; set; }
public Transaction InvoiceTransaction { get; set; }
public int TransactionID { get; set; }
}
public sealed class Transaction
{
public Transaction()
{
this.TransactionItems = new List<TransactionDetail>();
}
public int TransactionID { get; set; }
public DateTime TransactionDate { get; set; }
public DateTime TransactionLogDate { get; set; }
public TransactionType TransactionType { get; set; }
public IList<TransactionDetail> TransactionItems { get; set; }
public Invoice RefferingInvoice { get; set; }
public string Remarks { get; set; }
}
public sealed class TransactionDetail
{
public int TransactionID { get; set; }
public string ProductItemcode { get; set; }
public Product Product { get; set; }
public double Qty
{
get
{
return Math.Abs(this.StockChangeQty);
}
}
public double StockChangeQty { get; set; }
public double? UnitPrice { get; set; }
}
public sealed class Product
{
public Product()
{
this.StockTransactions = new List<TransactionDetail>();
}
public string ItemCode { get; set; }
public string ProductName { get; set; }
public string Manufacturer { get; set; }
public double UnitPrice { get; set; }
public IList<TransactionDetail> StockTransactions { get; set; }
public double Qty
{
get
{
if (this.StockTransactions.Count == 0)
{
return 0;
}
else
{
return this.StockTransactions.Sum(x => x.StockChangeQty);
}
}
}
public bool Discontinued { get; set; }
}
These are my view model classes;
public class InvoiceReportViewModel
{
public InvoiceReportViewModel()
{
LineItems = new List<InvoiceReportLineItemViewModel>();
}
public int InvoiceID { get; set; }
public DateTime InvoiceDate { get; set; }
public string CustomerName { get; set; }
public string CustomerAddress { get; set; }
public double? DiscountAmt { get; set; }
public string Remarks { get; set; }
public string StringInvoiceNo
{
get
{
return InvoiceID.ToString("########");
}
}
public IList<InvoiceReportLineItemViewModel> LineItems { get; set; }
}
public class InvoiceReportLineItemViewModel
{
public string ItemCode { get; set; }
public string ProductName { get; set; }
public string Manufacturer { get; set; }
public double? UnitPrice { get; set; }
public double Qty { get; set; }
public double LineTotal
{
get
{
if (UnitPrice.HasValue)
{
return UnitPrice.Value * Qty;
}
else
{
return 0;
}
}
}
}
My requirement is to convert the Invoice EF object to InvoiceReportViewModel object.
To do this I need to setup the profile. This is where I run into a problem; as it's not straight forward. The only way I see this done is by create my own Resolver by extending TypeConverter and manually doing the conversion by overriding ConvertCore method.
If there another way of getting this done (something with less work)???
Also I feel I could Map TransactionDetails EF class to InvoiceReportLineItemViewModel class by using the Mapper.CreateMap()..ForMember(...
But how can I use the mapper to convert it within the ConvertCore method?
Thanks in advance
In your case I do not see any requirements to use any custom converters.
You can convert Invoice EF object to InvoiceReportViewModel using simple Mapper.CreateMap like following:
public class InvoiceProfile: Profile
{
protected override void Configure()
{
Mapper.CreateMap<Invoice, InvoiceReportViewModel>()
.ForMember(c => c.CustomerName, op => op.MapFrom(v => v.CustomerName))
.ForMember(c => c.DiscountAmt, op => op.MapFrom(v => v.DiscountAmt))
.ForMember(c => c.InvoiceDate, op => op.MapFrom(v => v.InvoiceDate))
.ForMember(c => c.LineItems, op => op.MapFrom(v => v.InvoiceTransaction.TransactionItems));
Mapper.CreateMap<TransactionDetail, InvoiceReportLineItemViewModel>()
.ForMember(c => c.ProductName, op => op.MapFrom(v => v.Product.ProductName))
.ForMember(c => c.Qty, op => op.MapFrom(v => v.Qty))
//and so on;
}
}
Do not forget to register "InvoiceProfile"
I have the following classes
public class Group
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<GroupTier> Tiers { get; set; }
}
public class GroupTier : IEntity
{
public int Id { get; set; }
public int GroupId { get; set; }
public int Tier { get; set; }
public decimal Amount { get; set; }
public virtual Group Group { get; set; }
}
I am trying to map to the following ViewModel
public class GroupViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<decimal> Tiers { get; set; }
}
using the configuration
configuration.CreateMap<Group, GroupViewModel>()
.ForMember(m => m.Tiers, opt => opt.MapFrom(u => u.Tiers.OrderBy(q => q.Tier).Select(q => q.Amount)));
I am using EF6 to query from the database. I am having trouble when the Group.Tiers is null. How can I handle the null value?
When I use the this configuration
configuration.CreateMap<Group, GroupViewModel>()
.ForMember(m => m.Tiers, opt => opt.MapFrom(u => u.Tiers == null ? new List<decimal>() : u.Tiers.OrderBy(q => q.Tier).Select(q => q.Amount)));
I am getting this error
Cannot compare elements of type 'System.Collections.Generic.ICollection'
I'm having a little difficulty mapping a domain model to a view model, using AutoMapper.
My controller code is:
//
// GET: /Objective/Analyst
public ActionResult Analyst(int id)
{
var ovm = new ObjectiveVM();
ovm.DatePeriod = new DateTime(2013, 8,1);
var objectives = db.Objectives.Include(o => o.Analyst).Where(x => x.AnalystId == id).ToList();
ovm.ObList = Mapper.Map<IList<Objective>, IList<ObjectiveVM>>(objectives);
return View(ovm);
}
I am getting an error on the ovm.ObList = Mapper.... (ObList is underlined in red with the error):
'ObList': cannot reference a type through an expression; try 'Objectives.ViewModels.ObjectiveVM.ObList' instead
My Objective Class is:
public class Objective
{
public int ObjectiveId { get; set; }
public int AnalystId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public Analyst Analyst { get; set; }
}
My ObjectiveVM (view model) is:
public class ObjectiveVM
{
public DateTime DatePeriod { get; set; }
public class ObList
{
public int ObjectiveId { get; set; }
public int AnalystId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string AnalystName { get; set; }
public bool Include { get; set; }
}
}
In my startup/global.asax.cs I have used AutoMapper to map the Objective to the ObjectiveVM:
Mapper.CreateMap<Objective, ObjectiveVM.ObList>()
.ForMember(dest => dest.Include, opt => opt.Ignore())
.ForMember(dest => dest.AnalystName, opt => opt.MapFrom(y => (y.Analyst.AnalystName)));
Any help would be much appreciated,
Mark
Ok, thanks for all the suggestions - what I've ended up with is:
Controller:
//
// GET: /Objective/Analyst
public ActionResult Analyst(int id)
{
var ovm = new ObjectiveVM().obList;
var objectives = db.Objectives.Include(o => o.Analyst).Where(x => x.AnalystId == id).ToList();
ovm = Mapper.Map<IList<Objective>, IList<ObjectiveVM.ObList>>(objectives);
var ovm2 = new ObjectiveVM();
ovm2.obList = ovm;
ovm2.DatePeriod = new DateTime(2013, 8,1);
return View(ovm2);
}
ViewModel:
public class ObjectiveVM
{
public DateTime DatePeriod { get; set; }
public IList<ObList> obList { get; set; }
public class ObList
{
public int ObjectiveId { get; set; }
public int AnalystId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string AnalystName { get; set; }
public bool Include { get; set; }
}
}
CreateMap:
Mapper.CreateMap<Objective, ObjectiveVM.ObList>()
.ForMember(dest => dest.Include, opt => opt.Ignore())
.ForMember(dest => dest.AnalystName, opt => opt.MapFrom(y => (y.Analyst.AnalystName)))
;
If I've mis-understood any advice, and you provided the answer, please post it - and I'll mark it as such.
Thank you,
Mark
As the commenter nemesv has rightly mentioned, the problem is about
ovm.ObList = Mapper.Map<IList<Objective>, IList<ObjectiveVM>>(objectives);
ObList is not a member of ObjectiveVM so, you should change the ObjectiveVM like this:
public class ObjectiveVM
{
public DateTime DatePeriod { get; set; }
public IList<ObList> obList { get; set; }
public class ObList
{
public int ObjectiveId { get; set; }
public int AnalystId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string AnalystName { get; set; }
public bool Include { get; set; }
}
}
Update:
Controller:
public ActionResult Analyst(int id)
{
var ovm = new ObjectiveVM { DatePeriod = new DateTime(2013, 8, 1) };
var objectives = db.Objectives.Include(
o => o.Analyst).Where(x => x.AnalystId == id).ToList();
ovm.obList = Mapper.Map<IList<Objective>,
IList<ObjectiveVM.ObList>>(objectives);
return View(ovm);
}