AutoMapper.dll中发生了“AutoMapper.AutoMapperMappingException”,但未在用户代码中处理

时间:2014-02-15 13:58:53

标签: c# automapper

_quatationRepository = new QuatationRepository();
IList<Quatation> quatationObj = _quatationRepository.GetAll(quatation => quatation.IsDeleted == 0);
IList<QuatationModel> quatationModelObj = new List<QuatationModel>();

AutoMapper.Mapper.CreateMap<Quatation, QuatationModel>();
quatationModelObj = AutoMapper.Mapper.Map(quatationObj, quatationModelObj);
return quatationModelObj;

public partial class Quatation
{
    public int QuatationId { get; set; } 
    public int FirmId { get; set; } 
    public int ItemId { get; set; } 
    public double Quantity { get; set; } 
    public decimal Price { get; set; } 
    public Nullable Dated { get; set; } 
    public Nullable IsDeleted { get; set; } 
    public Nullable CreatedDate { get; set; } 
    public Nullable CreatedBy { get; set; } 
    public Nullable ModifyDate { get; set; } 
    public Nullable ModifyBy { get; set; }

    public virtual Firm Firm { get; set; }
    public virtual Item Item { get; set; }
}

public class QuatationModel
{
    public int QuatationId { get; set; } 
    public int? FirmId { get; set; } 
    public int ItemId { get; set; } 
    public double Quantity { get; set; } 
    public decimal Price { get; set; } 
    public DateTime? Dated { get; set; } 
    public int IsDeleted { get; set; } 
    public DateTime? CreatedDate { get; set; } 
    public int? CreatedBy { get; set; } 
    public DateTime? ModifyDate { get; set; } 
    public int? ModifyBy { get; set; }

    //public string ItemName { get; set; }
    //public List<SelectListItem> LstFirm { get; set; }


    public virtual FirmModel Firm { get; set; }
    public virtual ItemModel Item { get; set; }
    //public IList<QuatationModel> LstQuatation { get; set; }
}

3 个答案:

答案 0 :(得分:2)

您必须确保具有相同名称的属性具有相同的类型。例如,一边是int而另一边是Nullable(IsDeleted)。

答案 1 :(得分:0)

您可以将Quatation类的Nullable属性更改为:

public partial class Quatation
{
    public int QuatationId { get; set; } 
    public int FirmId { get; set; } 
    public int ItemId { get; set; } 
    public double Quantity { get; set; } 
    public decimal Price { get; set; }
    public DateTime? Dated { get; set; }
    public int IsDeleted { get; set; }
    public DateTime? CreatedDate { get; set; }
    public int? CreatedBy { get; set; }
    public DateTime? ModifyDate { get; set; }
    public int? ModifyBy { get; set; }

    public virtual Firm Firm { get; set; }
    public virtual Item Item { get; set; }
}

然后尝试映射即。

_quatationRepository = new QuatationRepository();
IList<Quatation> quatationObj = _quatationRepository.GetAll(quatation => quatation.IsDeleted == 0);
IList<QuatationModel> quatationModelObj = new List<QuatationModel>();

AutoMapper.Mapper.CreateMap<IList<Quatation>, IList<QuatationModel>>();
quatationModelObj = AutoMapper.Mapper.Map<IList<Quatation>, IList<QuatationModel>>(quatationObj.ToList());
return quatationModelObj;

答案 2 :(得分:0)

很抱歉迟到的回复 实际上由于我与另一个表的关系而发生错误。 我只是删除删除外键并更新我的edmx文件。 然后错误就解决了。

由于所有相关表中的数据不一致。

再次感谢您的回复。