在复杂类型中使用Automapper

时间:2013-09-24 23:28:23

标签: c# automapper

我有像EF这样的后端属性

 public partial class PatientSurveyQuestion
    {
        public PatientSurveyQuestion()
        {
            this.PatientSurveyQuestionAnswers = new HashSet<PatientSurveyQuestionAnswer>();
            this.PatientSurveyReplies = new HashSet<PatientSurveyReply>();
        }

        public int QuestionID { get; set; }
        public string QuestionText { get; set; }
        public Nullable<int> PointID { get; set; }
        public Nullable<int> QuestionType { get; set; }

        public virtual PatientSurveyPoint PatientSurveyPoint { get; set; }
        public virtual ICollection<PatientSurveyQuestionAnswer> PatientSurveyQuestionAnswers { get; set; }
        public virtual ICollection<PatientSurveyReply> PatientSurveyReplies { get; set; }
    }

  public partial class PatientSurveyQuestionAnswer
    {
        public int QuestionID { get; set; }
        public int AnswerID { get; set; }
        public Nullable<System.DateTime> DateCreayed { get; set; }

        public virtual PatientSurveyAnswer PatientSurveyAnswer { get; set; }
        public virtual PatientSurveyQuestion PatientSurveyQuestion { get; set; }
    }

和加入类

  public class PatientQS
    {

        public PatientSurveyQuestion Question { get; set; }
        public List<PatientSurveyAnswer> Answers { get; set; }
    }

我也有相同的类,没有WFC的引用和虚函数

[DataContract]
public class Question
{
    [DataMember]
    public int QuestionID { get; set; }
    [DataMember]
    public string QuestionText { get; set; }
    [DataMember]
    public Nullable<int> PointID { get; set; }
    [DataMember]
    public Nullable<int> QuestionType { get; set; }
}

   [DataContract]
    public class Answers
    {
        [DataMember]
        public int AnswerID { get; set; }
        [DataMember]
        public string Answer { get; set; }
    }

   [DataContract]
    public class QuestionsAnswers
    {
        [DataMember]
        public Question Question { get; set; }
        [DataMember]
        public List<Answers> Answers{ get; set; }
    }

然后我运行查询以获取数据并使用autopmaper

   var quesans = (from c in db.PatientSurveyQuestions
                       from v in db.PatientSurveyQuestionAnswers
                       where c.QuestionID == v.QuestionID && c.PointID == pointID
                       select new {c, ans=c.PatientSurveyQuestionAnswers.Select(n=>n.PatientSurveyAnswer)}).ToList().Select(c => new PatientQS { Question = c.c, Answers = c.ans.ToList() }).ToList();
                        Mapper.CreateMap<PatientSurveyAnswer, Answers>();
                        Mapper.CreateMap<PatientQS, QuestionsAnswers>().ForMember(c=>c.Answers, m=>m.MapFrom(
                              q=>Mapper.Map<List<PatientSurveyAnswer>, List<Answers>>(q.Answers)
                            ));
                        QuestionsAnswers q2 = Mapper.Map<QuestionsAnswers>(quesans);

但是automapper给了我一个错误

  

缺少类型映射配置或不支持的映射。

     

映射类型:   列表1 -> QuestionsAnswers System.Collections.Generic.List 1 [[PatientService1.PROP.PatientQS,PatientService1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]] - &gt; PatientService1.PROP.QuestionsAnswers

     

目的地路径:   QuestionsAnswers

     

来源价值:   System.Collections.Generic.List`1 [PatientService1.PROP.PatientQS]

我是做错了还是真的不支持?

0 个答案:

没有答案