Automapper to map child tables - automapper

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.

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.

Configuring AutoMapper in ASP.NET Core

I am trying to use automapper 8.0.0 to fill a list of WorkViewModel.
This list is getting data from the Work class out of the database using entity framework.
How ever it looks like something is going wrong with initializing as throwing follows error:
InvalidOperationException: Mapper not initialized
What am I doing wrong?
I have setup the following code!
Startup.cs
services.AddAutoMapper();
Function being called:
public async Task<IEnumerable<WorkViewModel>> GetAllAsync()
{
IList<Work> works = await _context.Work.ToListAsync();
IList<WorkViewModel> viewModelList = Mapper.Map<IList<Work>, IList<WorkViewModel>>(works);
return viewModelList;
}
Configuration:
public class MappingProfile : Profile
{
public MappingProfile()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<WorkViewModel, Work>();
});
}
}
WorkViewModel:
public class WorkViewModel
{
public int WorkID { get; set; }
public string Name { get; set; }
public byte[] Tumbmail { get; set; }
public string Discription { get; set; }
public string Client { get; set; }
public DateTime Date { get; set; }
public string PreviewLink { get; set; }
public string GitLink { get; set; }
public string DownloadLink { get; set; }
public int DetailID { get; set; }
public byte[] Banner { get; set; }
public string Documentation { get; set; }
public int CategoryID { get; set; }
public string Category { get; set; }
}
Work Model:
public class Work
{
[Key]
public int WorkID { get; set; }
[Display(Name = "Project Name")]
public string Name { get; set; }
[Display(Name = "Client name")]
public string Client { get; set; }
[Display(Name = "Description")]
public string Discription { get; set; }
[Display(Name = "Date")]
public DateTime Date { get; set; }
[Display(Name = "Thumbmail")]
public byte[] Tumbmail { get; set; }
[Display(Name = "Preview Link")]
public string PreviewLink { get; set; }
[Display(Name = "Git Link")]
public string GitLink { get; set; }
[Display(Name = "DownloadLink")]
public string DownloadLink { get; set; }
public WorkCategory workCategory { get; set; }
public WorkDetailed WorkDetailed { get; set; }
}
Only adding services.AddAutoMapper(); to the ConfigureServices method would not work for you. You have to configure AutoMapper as follows:
public void ConfigureServices(IServiceCollection services)
{
// Auto Mapper Configurations
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
services.AddMvc();
}
And also don't forget to install the AutoMapper.Extensions.Microsoft.DependencyInjection nuget package.

Determine column azure query result is from

I have this code:
var keyQuery = await MobileService.GetTable<Churches>()
.Where(item => item.CK_One == keyEntry.Text || item.CK_Two == keyEntry.Text || item.CK_Three == keyEntry.Text || item.CK_Four == keyEntry.Text || item.CK_Five == keyEntry.Text || item.CK_Six == keyEntry.Text || item.CK_Seven == keyEntry.Text || item.CK_Eight == keyEntry.Text || item.CK_Nine == keyEntry.Text || item.CK_Ten == keyEntry.Text)
.ToEnumerableAsync();
It searches ten different azure table columns for a match to the text entered by the user, how can I find out which of the ten columns the result came from?
This class defines Churches:
using System;
namespace ChurchBuilder
{
public class Churches
{
public string Id { get; set; }
public string Text { get; set; }
public string Church_Name { get; set; }
public bool Bulletin_Page { get; set; }
public bool Prayer_Page { get; set; }
public bool Sermons_Page { get; set; }
public bool Events_Page { get; set; }
public bool Messaging_Page { get; set; }
public bool Alert_Bar { get; set; }
public bool Push_Notifications { get; set; }
public string Master_Admin_Key { get; set; }
public string Admin_Key { get; set; }
public string Member_Key { get; set; }
public string CK_One { get; set; }
public string SA_One { get; set; }
public string CK_Two { get; set; }
public string SA_Two { get; set; }
public string CK_Three { get; set; }
public string SA_Three { get; set; }
public string CK_Four { get; set; }
public string SA_Four { get; set; }
public string CK_Five { get; set; }
public string SA_Five { get; set; }
public string CK_Six { get; set; }
public string SA_Six { get; set; }
public string CK_Seven { get; set; }
public string SA_Seven { get; set; }
public string CK_Eight { get; set; }
public string SA_Eight { get; set; }
public string CK_Nine { get; set; }
public string SA_Nine { get; set; }
public string CK_Ten { get; set; }
public string SA_Ten { get; set; }
public string Email { get; set; }
public double CostPerMonth { get; set; }
public bool RolePageOne { get; set; }
public bool RolePageTwo { get; set; }
public bool RolePageThree { get; set; }
public bool RolePageFour { get; set; }
public bool RolePageFive { get; set; }
public bool RolePageSix { get; set; }
public bool RolePageSeven { get; set; }
public bool RolePageEight { get; set; }
public bool RolePageNine { get; set; }
public bool RolePageTen { get; set; }
public int Custom_Roles { get; set; }
public string Custom_Name_One { get; set; }
public string Custom_Name_Two { get; set; }
public string Custom_Name_Three { get; set; }
public string Custom_Name_Four { get; set; }
public string Custom_Name_Five { get; set; }
public string Custom_Name_Six { get; set; }
public string Custom_Name_Seven { get; set; }
public string Custom_Name_Eight { get; set; }
public string Custom_Name_Nine { get; set; }
public string Custom_Name_Ten { get; set; }
public Churches()
{
}
}
}
You can use reflection to match the key and that should be able to give your the property/column name that was matched
var key = keyEntry.Text;
var prefix = "CK_";
var church = keyQuery.FirstOrDefault();
if (church != null) {
var type = church.GetType();
var columnName = type.GetProperties()
.Where(property =>
property.Name.StartsWith(prefix) && property.GetValue(church) == key
).First().Name;
}
The prefix is used to narrow the search of the properties that were used in the filter.

Automapper is mapping incorrectly?

I have two objects, Project is a DB entity and IProject which I am trying to save to the database:
public interface IProject
{
int ProjectID { get; set; }
string JobNumber { get; set; }
string JobName { get; set; }
string ProjectLocation { get; set; }
int? FeeTypeID { get; set; }
decimal? Fee { get; set; }
decimal? ConstructionCost { get; set; }
decimal? CostPerSquareFoot { get; set; }
decimal? SquareFoot { get; set; }
string Memo { get; set; }
DateTime? DateCompleted { get; set; }
int JobStatusID { get; set; }
int ProjectTypeID { get; set; }
}
//The inheritance is actually in a partial class that doesn't get overridden
public partial class Project : IProject
{
public Project()
{
this.ProjectContacts = new HashSet<ProjectContact>();
this.ProjectConsultants = new HashSet<ProjectConsultant>();
}
public int ProjectID { get; set; }
public string JobNumber { get; set; }
public string JobName { get; set; }
public Nullable<int> FeeTypeID { get; set; }
public Nullable<decimal> Fee { get; set; }
public int JobStatusID { get; set; }
public string Memo { get; set; }
public Nullable<decimal> Acreage { get; set; }
public Nullable<decimal> SquareFoot { get; set; }
public Nullable<decimal> ConstructionCost { get; set; }
public Nullable<decimal> BudgetPrice { get; set; }
public Nullable<decimal> ActualPrice { get; set; }
public Nullable<System.DateTime> DateCompleted { get; set; }
public Nullable<decimal> CostPerSquareFoot { get; set; }
public string ProjectLocation { get; set; }
public int ProjectTypeID { get; set; }
public Nullable<System.DateTime> StartDate { get; set; }
public Nullable<int> PhaseID { get; set; }
public string ProjectDescription { get; set; }
public Nullable<int> ArchitectID { get; set; }
public Nullable<int> ManagerID { get; set; }
public Nullable<int> ArchiveLocationID { get; set; }
public virtual ICollection<ProjectContact> ProjectContacts { get; set; }
public virtual FeeType FeeType { get; set; }
public virtual JobStatu JobStatu { get; set; }
public virtual ICollection<ProjectConsultant> ProjectConsultants { get; set; }
public virtual Person Person { get; set; }
public virtual ArchiveLocation ArchiveLocation { get; set; }
public virtual Person Person1 { get; set; }
public virtual Phase Phase { get; set; }
public virtual ProjectType ProjectType { get; set; }
}
I have my Map created:
Mapper.CreateMap<IProject, Data.Project>();
but for some reason whenever I call the map:
//mappingService is a wrapper I have around Automapper so that I can inject it
var project = _mappingService.Map<IProject, Data.Project>(request);
I get this error:
Missing type map configuration or unsupported mapping.
Mapping types:
RuntimeType -> FeeType
System.RuntimeType -> Renaissance.Data.FeeType
Destination path:
Project.FeeType.FeeType
Source value:
System.Decimal
I am not at any point trying to do that. The names match and even if I specifically tell it to map with:
.ForMember(dest => dest.Fee, opt => opt.MapFrom(src => src.Fee))
It still errors with the same message.
What am I missing?
UPDATE
This works:
.ForMember(dest => dest.FeeType, opt => opt.Ignore());
I think it is a hack that I need to do that. I shouldn't have to ignore a property in the destination that does not exist / is not named the same as something in the source property.

ServiceStack ORMLite - Can't Infer Relationship (because of DTO name?)

I am modeling a service business that performs multiple services at each visit to a client. I have a Visit table, a Service table and a link table, VisitService. I am using this Request DTO in my service to get a list of services for a visit:
[Route("/visits/{visitid}/services", Verbs = "GET")]
public class ServicesAtVisit : QueryBase<VisitService, ServiceAtVisit>, IJoin<VisitService, My.Namespace.Service> {
public int VisitId { get; set; }
}
ServiceAtVisit is a custom DTO that I'm projecting into.
Because one of my DTOs is a class with the unfortunate name "Service", I have to fully-qualify it in the IJoin because, otherwise, it is ambiguous with ServiceStack.Service. Now, when I hit the route, I get the error "Could not infer relationship between VisitService and Service".
The interesting thing is that I've got this working with other many-to-many relationships (Client.AssignedStaffMembers, StaffMember.AssignedClients for the tables Client -> ClientStaffMember -> StaffMember) and I can't see anything different.
Is the problem the name of my DTO and the fact that I'm having to fully-qualify it?
Visit:
[Route("/visits", Verbs = "POST")]
public partial class Visit {
[AutoIncrement]
public long Id { get; set; }
public int ServiceRequestId { get; set; }
public string TimeOfDay { get; set; }
public DateTime Date { get; set; }
public TimeSpan? PreferredStartTime { get; set; }
public TimeSpan? PreferredEndTime { get; set; }
public bool IsFirstVisit { get; set; }
public bool IsLastVisit { get; set; }
public bool IncursWeekendFee { get; set; }
public bool WaiveWeekendFee { get; set; }
public bool IncursHolidayFee { get; set; }
public bool WaiveHolidayFee { get; set; }
public bool IncursLastMinuteSchedulingFee { get; set; }
public bool WaiveLastMinuteSchedulingFee { get; set; }
public bool IncursLastMinuteCancellationFee { get; set; }
public bool WaiveLastMinuteCancellationFee { get; set; }
public int? StaffMemberId { get; set; }
public string Notes { get; set; }
public bool IsCancelled { get; set; }
public DateTime? CheckInDateTime { get; set; }
public int? CheckInStaffMemberId { get; set; }
public DateTime? CheckOutDateTime { get; set; }
public int? CheckOutStaffMemberId { get; set; }
[Ignore]
public ServiceRequest ServiceRequest { get; set; }
[Ignore]
public StaffMember StaffMember { get; set; }
[Ignore]
public List<ServiceAtVisit> ServicesAtVisit { get; set; }
}
VisitService:
public partial class VisitService {
// Workaround for composite key limitation
public string Id {
get {
return this.VisitId.ToString() + "|" + this.ServiceId.ToString();
}
}
public long VisitId { get; set; }
public int ServiceId { get; set; }
public int Quantity { get; set; }
public bool FeeIsWaived { get; set; }
[Ignore]
public Visit Visit { get; set; }
[Ignore]
public Service Service { get; set; }
}
Service:
public partial class Service {
[AutoIncrement]
public int Id { get; set; }
public int ServiceTypeId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int DisplayOrder { get; set; }
public bool IsHourly { get; set; }
public bool IsMonthly { get; set; }
[Ignore]
public ServiceType ServiceType { get; set; }
[Ignore]
public ServicePrice CurrentPrice { get; set; }
}
Projecting results into ServiceAtVisit:
public partial class ServiceAtVisit {
public int ServiceTypeId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int DisplayOrder { get; set; }
public bool IsHourly { get; set; }
public bool IsMonthly { get; set; }
public int VisitId { get; set; }
public int ServiceId { get; set; }
public int Quantity { get; set; }
public bool FeeIsWaived { get; set; }
}
I've just tried creating an AutoQuery Service with all the types you've provided under a custom MyNamespace and it's working as expected (in the latest of ServiceStack), i.e:
Service definition:
namespace MyNamespace
{
[Route("/visits/{VisitId}/services", Verbs = "GET")]
public class ServicesAtVisit : QueryBase<VisitService, ServiceAtVisit>,
IJoin<VisitService, Service>
{
public int VisitId { get; set; }
}
public partial class ServiceAtVisit
{
public int ServiceTypeId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int DisplayOrder { get; set; }
public bool IsHourly { get; set; }
public bool IsMonthly { get; set; }
public int VisitId { get; set; }
public int ServiceId { get; set; }
public int Quantity { get; set; }
public bool FeeIsWaived { get; set; }
}
}
Type definition:
namespace MyNamespace
{
[Route("/visits", Verbs = "POST")]
public partial class Visit
{
[AutoIncrement]
public long Id { get; set; }
public int ServiceRequestId { get; set; }
public string TimeOfDay { get; set; }
public DateTime Date { get; set; }
public TimeSpan? PreferredStartTime { get; set; }
public TimeSpan? PreferredEndTime { get; set; }
public bool IsFirstVisit { get; set; }
public bool IsLastVisit { get; set; }
public bool IncursWeekendFee { get; set; }
public bool WaiveWeekendFee { get; set; }
public bool IncursHolidayFee { get; set; }
public bool WaiveHolidayFee { get; set; }
public bool IncursLastMinuteSchedulingFee { get; set; }
public bool WaiveLastMinuteSchedulingFee { get; set; }
public bool IncursLastMinuteCancellationFee { get; set; }
public bool WaiveLastMinuteCancellationFee { get; set; }
public int? StaffMemberId { get; set; }
public string Notes { get; set; }
public bool IsCancelled { get; set; }
public DateTime? CheckInDateTime { get; set; }
public int? CheckInStaffMemberId { get; set; }
public DateTime? CheckOutDateTime { get; set; }
public int? CheckOutStaffMemberId { get; set; }
//[Ignore]
//public ServiceRequest ServiceRequest { get; set; }
//[Ignore]
//public StaffMember StaffMember { get; set; }
[Ignore]
public List<ServiceAtVisit> ServicesAtVisit { get; set; }
}
public partial class VisitService
{
// Workaround for composite key limitation
public string Id
{
get
{
return this.VisitId.ToString() + "|" + this.ServiceId.ToString();
}
}
public long VisitId { get; set; }
public int ServiceId { get; set; }
public int Quantity { get; set; }
public bool FeeIsWaived { get; set; }
[Ignore]
public Visit Visit { get; set; }
[Ignore]
public Service Service { get; set; }
}
public partial class Service
{
[AutoIncrement]
public int Id { get; set; }
public int ServiceTypeId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int DisplayOrder { get; set; }
public bool IsHourly { get; set; }
public bool IsMonthly { get; set; }
//[Ignore]
//public ServiceType ServiceType { get; set; }
//[Ignore]
//public ServicePrice CurrentPrice { get; set; }
}
}
Test Data:
using (var db = container.Resolve<IDbConnectionFactory>().Open())
{
db.DropAndCreateTable<MyNamespace.Visit>();
db.DropAndCreateTable<MyNamespace.VisitService>();
db.DropAndCreateTable<MyNamespace.Service>();
var visitId = db.Insert(new MyNamespace.Visit {
Date = DateTime.UtcNow, Notes = "Notes"}, selectIdentity: true);
var serviceId = (int)db.Insert(new MyNamespace.Service {
ServiceTypeId = 1, Name = "Name"}, selectIdentity:true);
db.Insert(new MyNamespace.VisitService {
VisitId = visitId, ServiceId = serviceId, Quantity = 1});
}
Service Result:
http://localhost:{port}/visits/1/services.json
JSON Response:
{
offset: 0,
total: 1,
results: [{
serviceTypeId: 1,
name: "Name",
displayOrder: 0,
isHourly: false,
isMonthly: false,
visitId: 1,
serviceId: 1,
quantity: 1,
feeIsWaived: false
}]
}

Resources