AutoMapper "Member not found" with UseDestinationValue - c#

I am trying to use AutoMapper to map a ViewModel to a Model.
Here is my simplified ViewModel (the source) class:
public class EditPaypointVM
{
public Int64 Id { get; set; }
public Int64 OrganisationId { get; set; }
[Required]
public string OrganisationContactNumber { get; set; }
public Int64 PostalAddressId { get; set; }
public string PostalAddressAddressText { get; set; }
[Required]
public Int64 PostalAddressArea { get; set; }
public string PostalAddressAreaText { get; set; }
public string PostalAddressCode { get; set; }
public Int64 PhysicalAddressId { get; set; }
public string PhysicalAddressAddressText { get; set; }
[Required]
public Int64 PhysicalAddressArea { get; set; }
public string PhysicalAddressAreaText { get; set; }
public string PhysicalAddressCode { get; set; }
}
Here is my simplified Model (destination) class:
public class Paypoint
{
public Int64 Id { get; set; }
public virtual Organisation Organisation { get; set; }
public virtual Employer Employer { get; set; }
public virtual List<EmploymentContract> EmploymentContracts { get; set; }
public bool IsActive { get; set; }
}
public class Organisation
{
public Int64 Id { get; set; }
public virtual List<EmailAddress> EmailAdresses { get; set; }
public virtual List<ContactNumber> ContactNumbers { get; set; }
public virtual List<Address> Adresses { get; set; }
public string RegisteredName { get; set; }
public string TradingName { get; set; }
public string RegistrationNumber { get; set; }
public string WebsiteAddress { get; set; }
}
Here is the code I execute to create mappings in memory on application start:
Mapper.CreateMap<EditPaypointVM, Paypoint>()
.ForMember(dest => dest.IsActive,
opt => opt.UseValue(true))
.ForMember(dest => dest.Organisation,
opt => opt.UseDestinationValue())
.Ignore(i => i.Employer)
.Ignore(i => i.EmploymentContracts);
Upon executing 'AssertConfigurationIsvalid' within a unit test, an "Unmapped error is thrown which states that the Organisation member of Paypoint is unmapped.
Any ideas on what causes this?

Related

How to properly map Entity Framework entities to DTO and vice-versa?

In the data access level, I have defined such an entity:
public class Instagram
{
public int Id { get; set; }
public string Username { get; set; } = null!;
public string Password { get; set; } = null!;
public string? StateData { get; set; }
public string? TwoFactorLoginInfo { get; set; }
public string? ChallengeLoginInfo { get; set; }
public bool IsActive { get; set; }
public bool IsSelected { get; set; }
public long UserId { get; set; }
public User User { get; set; } = null!;
public Proxy? Proxy { get; set; }
public int? ProxyId { get; set; }
public List<Work>? Works { get; set; }
}
At the abstraction level, there is such a DTO:
public class InstagramDto
{
public int Id { get; set; }
public string Username { get; set; } = null!;
public string Password { get; set; } = null!;
public string? StateData { get; set; }
public string? TwoFactorLoginInfo { get; set; }
public string? ChallengeLoginInfo { get; set; }
public bool IsSelected { get; set; }
public bool IsActive { get; set; }
public UserDto? User { get; set; }
public ProxyDto? Proxy { get; set; }
}
Did I form the DTO correctly? It maps well to one side (from EF to DTO), but the problem is reverse mapping. I do it like this:
CreateMap<InstagramDto, Instagram>()
.ForMember(x => x.UserId,
expression => expression.MapFrom((dto, _) => dto.User?.Id))
.ForMember(x => x.User, expression => expression.Ignore())
.ForMember(x => x.Proxy,
expression => expression.MapFrom((dto, _) => dto.Proxy?.Id))
.ForMember(x => x.Proxy, expression => expression.Ignore());
CreateMap<Instagram, InstagramDto>();
That is, InstagramDto must have UserDTO and ProxyDto so that I can correctly map the entity from DTO to ef. At the same time, the user may have some other navigation properties that are not involved when receiving Instagram. This means that I cannot update the User, as its navigation properties are not loaded in this situation. Is this the right approach or would it be better to do so:
public int Id { get; set; }
public string Username { get; set; } = null!;
public string Password { get; set; } = null!;
public string? StateData { get; set; }
public string? TwoFactorLoginInfo { get; set; }
public string? ChallengeLoginInfo { get; set; }
public bool IsSelected { get; set; }
public bool IsActive { get; set; }
public long UserId { get; set; }
public int ProxyId { get; set; }

AutoMapperMappingException: Missing type map configuration or unsupported mapping

I'm attempting to use AutoMapper for the first time. The GSMSite maps fine, but I get an error when trying to map the GSMUnit:
AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Do I need to specify a relationship between GSMSite and GSMUnits?
Domain class:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Company { get; set; }
public string Telephone { get; set; }
public bool PasswordReset { get; set; } = false;
public virtual ICollection<GSMSite> GSMSites { get; set; }
}
public class GSMSite
{
public int Id { get; set; }
public string SiteName { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string Town { get; set; }
public string Postcode { get; set; }
public string ContactName { get; set; }
public string ContactNumber { get; set; }
public string ContactEmail { get; set; }
public string ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public virtual ICollection<GSMUnit> GSMUnits { get; set; }
}
public class GSMUnit
{
public int Id { get; set; }
public string Model { get; set; }
public string Firmware { get; set; }
public string TelephoneNum { get; set; }
public int GSMSiteId { get; set; }
public virtual GSMSite GSMSite { get; set; }
}
Contract class:
public class GSMResponse
{
public int Id { get; set; }
public string Model { get; set; }
public string Firmware { get; set; }
public string TelephoneNum { get; set; }
}
public class SiteResponse
{
public int Id { get; set; }
public string SiteName { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string Town { get; set; }
public string Postcode { get; set; }
public string ContactName { get; set; }
public string ContactNumber { get; set; }
public string ContactEmail { get; set; }
}
Mapping:
public DomainToResponseProfile()
{
CreateMap<GSMSite, SiteResponse>();
CreateMap<GSMUnit, GSMResponse>();
}

Invalid column name: "ColumnName#" with a suffix number

I have a table Users:
I am using Entity Framework 6 to make the type configuration
public class UsersMapping : EntityTypeConfiguration<Users>
{
public UsersMapping()
{
HasKey(t => t.UserID);
Property(t => t.UserID).IsRequired();
ToTable("Users", "dbo");
Property(t => t.Id).HasColumnName("UserID");
}
}
and this is the Users class:
public class Users : EntityBase
{
public int UserID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public DateTime DateCreated { get; set; }
public byte[] Timestamp { get; set; }
public byte[] Password { get; set; }
public DateTime? DateActivated { get; set; }
public bool LockedOut { get; set; }
public string Address { get; set; }
public string City { get; set; }
public int? State { get; set; }
public string Zip { get; set; }
public string SecurityQuestion { get; set; }
public string SecurityAnswer { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public bool? ReportServiceAccount { get; set; }
public string CompanyName { get; set; }
public int? ILAC { get; set; }
public string BioFingerprint { get; set; }
public string Title { get; set; }
public string Squad { get; set; }
public byte[] SignatureFile { get; set; }
public byte? CETGroupID { get; set; }
public string TokenID { get; set; }
public int? Pin { get; set; }
public string RadioNr { get; set; }
}
public class EntityBase
{
public int Id { get; set; }
}
I am trying to set the Id field as primary because my repository pattern works using the field Id that is not present on this case in the table. But when I am trying to get the set of users I get this error:
The column name is invalid UserID1
The weird thing to me is the #1 that is at the end. What I am doing wrong?

How to use Fluent API to map foreign key

I am trying to use modelBuilder to map a foreign Key in my database. I need to post the Id into JobTESPM_EmployeeId instead of JOBTESPMId. I have been using this a guide but I can't see where the examples are the same as my setup. http://msdn.microsoft.com/en-in/data/jj591620.aspx#IndependentAssociation
public class Job
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
public Int64? JobId { get; set; }
public int? JobNumber { get; set; }
public string JobName { get; set; }
public int? JobTypeId { get; set; }
public int? CustomerEmployeePMId { get; set; }
public virtual CustomerEmployee CustomerEmployeePM { get; set; }
public int? CustomerEmployeeAdminId { get; set; }
public virtual CustomerEmployee CustomerEmployeeAdmin { get; set; }
public int? CustomerEmployeeAccountantId { get; set; }
public virtual CustomerEmployee CustomerEmployeeAccountant { get; set; }
public int? CustomerEmployeeSuperintendentId { get; set; }
public virtual CustomerEmployee CustomerEmployeeSuperintendent { get; set; }
public int? JobTESPMId { get; set; }
public virtual Employee JobTESPM { get; set; }
public int? JobTESSuperintendentId { get; set; }
public virtual Employee JobTESSuperintendent { get; set; }
}
public class Employee
{
public int EmployeeId { get; set; }
public string AccountName { get; set; }
public string EmployeeFirstName { get; set; }
public string EmployeeLastName { get; set; }
public string EmployeeTitle { get; set; }
public Int64? EmployeeCellPhone { get; set; }
public Int64? EmployeeOfficePhone { get; set; }
public string EmployeeEmail { get; set; }
public int? CompanyEmployeeId { get; set; }
public bool? EmployeeHidden { get; set; }
public bool? EmployeeIsSuper { get; set; }
public bool? EmployeeIsPM { get; set; }
public string EmployeeAltEmail { get; set; }
}
What about adding a couple attributes...
[Column("JobTESPM_EmployeeID")]
public int? JobTESPMId { get; set; }
[ForeignKey("JobTESPMId")]
public virtual Employee JobTESPM { get; set; }

How to write a many-to-many relationship in Fluent in MVC4 and Entity Code First

I'm trying to write a many-to-many relationship in the override of the onModelCreating method of my context in ASP.NET MVC4. I think I have my classes wrong because I'm getting errors in Intellisense that I don't understand. Here is my override:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Software>().
HasMany(i => i.LocationId).
WithMany(c => c.SoftwareId).
Map(mc =>
{
mc.MapLeftKey("SoftwareId");
mc.MapRightKey("LocationId");
mc.ToTable("SoftwareLocations");
});
}
Here is my Software class:
public class Software
{
public int Id { get; set; }
public virtual List<SoftwareType> SoftwareTypes { get; set; }
public virtual List<Location> Locations { get; set; }
public virtual List<SoftwarePublisher> Publishers { get; set; }
[Required]
[StringLength(128)]
public string Title { get; set; }
[Required]
[StringLength(10)]
public string Version { get; set; }
[Required]
[StringLength(128)]
public string SerialNumber { get; set; }
[Required]
[StringLength(3)]
public string Platform { get; set; }
[StringLength(1000)]
public string Notes { get; set; }
[Required]
[StringLength(15)]
public string PurchaseDate { get; set; }
public bool Suite { get; set; }
public string SubscriptionEndDate { get; set; }
//[Required]
//[StringLength(3)]
public int SeatCount { get; set; }
public virtual Location LocationId { get; set; }
}
Here is my Location class:
public class Location
{
public int Id { get; set; }
[Required]
[StringLength(20)]
public string LocationName { get; set; }
public virtual Software SoftwareId { get; set; }
}
How do I write my Fluent override so I can map them correctly?
Many-to-Many means that in both entities is Collections. So you should set HasMany(software=>software.Locations) and set WithMany(location=>location.Softwares) as in example:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Software>().
HasMany(i => i.Locations).
WithMany(c => c.Softwares).
Map(mc =>
{
mc.MapLeftKey("SoftwareId");
mc.MapRightKey("LocationId");
mc.ToTable("SoftwareLocations");
});
}
Here is my Software class:
public class Software
{
public int Id { get; set; }
public virtual List<SoftwareType> SoftwareTypes { get; set; }
public virtual ICollection <Location> Locations { get; set; }
public virtual List<SoftwarePublisher> Publishers { get; set; }
[Required]
[StringLength(128)]
public string Title { get; set; }
[Required]
[StringLength(10)]
public string Version { get; set; }
[Required]
[StringLength(128)]
public string SerialNumber { get; set; }
[Required]
[StringLength(3)]
public string Platform { get; set; }
[StringLength(1000)]
public string Notes { get; set; }
[Required]
[StringLength(15)]
public string PurchaseDate { get; set; }
public bool Suite { get; set; }
public string SubscriptionEndDate { get; set; }
//[Required]
//[StringLength(3)]
public int SeatCount { get; set; }
}
Here is my Location class:
public class Location
{
public int Id { get; set; }
[Required]
[StringLength(20)]
public string LocationName { get; set; }
public virtual ICollection<Software> Softwares { get; set; }
}

Categories