Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have some issues with automapper.

I have two objects and I want to convert these.

First object;

C#
public partial class WorkCommandDocuments
    {
        public int ID { get; set; }

        [Required]
        [StringLength(300)]
        public string WorkCommandNumber { get; set; }

        [Required]
        [StringLength(500)]
        public string WorkCommandDocumentDefinition { get; set; }

        [Required]
        public byte[] WorkCommandDocument { get; set; }

        [Required]
        [StringLength(100)]
        public string FileExtension { get; set; }

        public virtual WorkCommands WorkCommands { get; set; }
    }


Second object;

C#
public class WorkCommandDoc
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string WorkCommandNumber { get; set; }
        [DataMember]
        public string WorkCommandDocumentDefinition { get; set; }
        [DataMember]
        public byte[] WorkCommandDocument { get; set; }
        [DataMember]
        public string FileExtension { get; set; }

        public WorkCommandDoc(int id, string workCommandNumber, string workCommandDocumentDefinition, byte[] workCommandDoc, string fileExtension)
        {
            ID = id;
            WorkCommandNumber = workCommandNumber;
            WorkCommandDocumentDefinition = workCommandDocumentDefinition;
            WorkCommandDocument = workCommandDoc;
            FileExtension = fileExtension;
        }
    }


But I'm getting 'AutoMapper.AutoMapperConfigurationException' in AutoMapper.dll error.

EDIT: I solve this problem with adding default constractor to WorkCommandDoc object.

What I have tried:

I tried:

C#
Mapper.Initialize(config => 
            {
                config.CreateMap<WorkCommandDocuments, WorkCommandDoc>();
            });
            Mapper.AssertConfigurationIsValid();

 WorkCommandDoc wc = Mapper.Map<WorkCommandDoc>(doc);
Posted
Updated 24-Jan-18 9:46am
v2
Comments
[no name] 24-Jan-18 7:44am    
I don't see any class for the below code.I believe there are some versioning issue.
Hide   Copy Code
public virtual WorkCommands WorkCommands { get; set; }
Onur ERYILMAZ 24-Jan-18 7:53am    
Thank you for your response.

Class WorkCommands is exists but I don't want to transfer this class, I only want to transfer ID, WorkCommandNumber, WorkCommandDocumentDefinition, WorkCommandDocument and FileExtension properties.
[no name] 24-Jan-18 8:13am    
Use automapper version 3.1.1 and write like below.Check out this link:https://dotnetfiddle.net/fSMiLR

WorkCommandDoc WorkCommandDoc = new WorkCommandDoc(12,"2391283k","Doc64",new byte[obj.Length],"docx");      Mapper.CreateMap<WorkCommandDoc, WorkCommandDocuments>();				WorkCommandDocuments cvm = Mapper.Map<WorkCommandDoc, WorkCommandDocuments>(WorkCommandDoc);				Console.WriteLine(cvm.ID);

Don't copy paste the code it will throw you errors.The issue that you are getting must be a auto mapper version related issue.
Onur ERYILMAZ 24-Jan-18 8:47am    
Unfortunately this is not working either :(
[no name] 24-Jan-18 9:04am    
Please post the complete stack trace of error that you are getting

1 solution

EDIT: I solve this problem with adding default constractor to WorkCommandDoc object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900