Base solution for your next web application
Open Closed

Error Mapping DTO to Domain Object #175


User avatar
0
mpm created

I've created a unit test for creating a device object with a DeviceName, Site, and IsTraining bit. AutoMapper is throwing out the following error in the test. I'm not sure why my DTO and Domain object aren't able to map properly.

Result Message: AutoMapper.AutoMapperMappingException : Missing type map configuration or unsupported mapping.

Mapping types: DeviceEditDto -> Device MCMT.PatientCareReporting.Devices.Dto.DeviceEditDto -> MCMT.PatientCareReporting.Devices.Device

My test looks like so:

public class DeviceAppService_Create_Tests : DeviceAppServiceTestBase
    {
        [Fact]
        public async Task Should_Create_Device()
        {
            LoginAsHostAdmin();

            await CreateDeviceAndTestAsync("TESTDEVICE", "Rochester", false);
            await CreateDeviceAndTestAsync("R5001011", "Austin", true);
        }

        private async Task CreateDeviceAndTestAsync(string deviceName, string site, bool isTraining)
        {
            //Act
            await DeviceAppService.CreateOrUpdateDevice(
                new CreateOrUpdateDeviceInput
                {
                    Device = new DeviceEditDto
                    {
                        DeviceName = deviceName,
                        Site = site,
                        IsTraining = isTraining
                    }
                });

            //Assert
            UsingDbContext(async context =>
            {
                //Get created device
                var createdDevice = await context.Devices.FirstOrDefaultAsync(u => u.DeviceName == "TESTDEVICE");
                createdDevice.ShouldNotBe(null);

                //Check some properties
                createdDevice.Site.ShouldBe("Rochester");
                createdDevice.IsTraining.ShouldBe(false);
            });
        }
    }

My domain object is as follows:

[Table("rntDevice")]
    public class Device : FullAuditedEntity
    {
        public const int MaxDeviceNameLength = 10;
        public const int MaxSiteLength = 32;
        
        [Required]
        [MaxLength(MaxDeviceNameLength)]
        public virtual string DeviceName { get; set; }

        [Required]
        [MaxLength(MaxSiteLength)]
        public virtual string Site { get; set; }
 
        public virtual bool IsTraining { get; set; }
    }

and the DTO object looks like so:

public class DeviceEditDto : IValidate
    {
        /// <summary>
        /// Set null to create a new device. Set device's Id to update a device
        /// </summary>
        public long? Id { get; set; }

        [Required]
        [StringLength(Device.MaxDeviceNameLength)]
        public string DeviceName { get; set; }

        [Required]
        [StringLength(Device.MaxSiteLength)]
        public string Site { get; set; }

        public bool IsTraining { get; set; }
    }

Any thoughts I what I might have wired together incorrectly?


1 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    As I see, you haven't defined mapping between DTO and Entity.

    You can simply do it by adding an attribute to DTO class like below:

    [AutoMap(typeof(Device))]
    

    See other DTOs in the application, they have mappings.

    Also, I advice to check AutoMapper's documentation to have a deep understanding of object-to-object mapping.

    Have a nice day :)