我需要通过 autompper 将可空 int 映射到可空 int
这是我的类(class)事件:
public class Incident : Evenement
{
public Incident()
: base(-1, Global.EvenementType.Incidents, DateTime.MinValue)
{
}
public Incident(int id, DateTime date, string libelle, int formulaireId, string societe, string userName)
: base(id, (int)Global.EvenementType.Incidents, date, libelle, "", "", societe)
{
ContratId = formulaireId;
Contrat = null;
UserName = userName;
}
public Incident(int id, DateTime date, string libelle, string societe, string userName)
: base(id, (int)Global.EvenementType.Incidents, date, libelle, "", "", societe)
{
ContratId = null;
Contrat = null;
UserName = userName;
}
[DataMember]
public int? ContratId { get; set; }
[DataMember]
public Contrat Contrat { get; set; }
[DataMember]
public string UserName { get; set; }
[DataMember]
public bool IsGeneral
这是我类(class)的事件
public partial class INCIDENT : EVENEMENT
{
public Nullable<int> FORMULAIRE_ID { get; set; }
public virtual FORMULAIRE FORMULAIRE { get; set; }
public string USERNAME { get; set; }
}
当我在做映射时,contratId dans 事件中有一个 null,它会自动转换为事件类 FORMULAIRE_ID 中的 0
这是我的绑定(bind)
Mapper.CreateMap<Incident, INCIDENT>()
.ForMember(degivreuse => degivreuse.FORMULAIRE_ID, expression => expression.MapFrom(degivreuse => degivreuse.ContratId))
.ForMember(degivreuse => degivreuse.FORMULAIRE, expression => expression.Ignore());
而在 PJ 中,问题是:
你知道为什么我没有获得空值吗?
问候
最佳答案
我没有发现 Automapper 处理 int?
的方式有任何问题。这是一个运行良好的快速示例:
public class Src1
{
public string Name { get; set; }
public int? Age { get; set; }
}
public class Dest1
{
public string Name { get; set; }
public Nullable<int> Age { get; set; }
}
Mapper.CreateMap<Src1, Dest1>();
Mapper.AssertConfigurationIsValid();
var s = new Src1 {Name = "aaa", Age = null};
var d = Mapper.Map<Src1, Dest1>(s);
在上面的示例中,d.Age
为 null
。您能否提供一些示例代码来重现您遇到的问题?
https://stackoverflow.com/questions/33287550/