【发布时间】:2016-01-22 02:48:33
【问题描述】:
我需要通过 autompper 将一个可为空的 int 映射到一个可为空的 int
这是我的班级事件:
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
这是我的班级事件
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 时,它会在 INCIDENT 类的 FORMULAIRE_ID 中自动转换为 0
这是我的绑定
Mapper.CreateMap<Incident, INCIDENT>()
.ForMember(degivreuse => degivreuse.FORMULAIRE_ID, expression => expression.MapFrom(degivreuse => degivreuse.ContratId))
.ForMember(degivreuse => degivreuse.FORMULAIRE, expression => expression.Ignore());
您知道为什么我没有获得空值吗?
问候
【问题讨论】:
-
您的代码应该成功地将 null 映射到 null。我将从检查
FORMULAIRE_ID的所有写入用法开始。也许它的值在属性映射后被改变(在另一个属性设置器或其他东西中?)。 -
不,无论如何都不会改变!!!
标签: c# entity-framework mapping automapper