将nullable int映射到nullable int + automapper

时间:2015-10-22 17:58:06

标签: c# entity-framework mapping automapper

我需要通过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

这是我的课程INCIDENT

 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事件中有空时,它在类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());

在PJ问题中: enter image description here

你知道为什么我没有获得空值吗?

此致

2 个答案:

答案 0 :(得分:0)

我没有看到Automapper处理utm_campaign的方式有任何问题。这是一个很好的快速样本:

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。您能否提供一些示例代码来重现您所看到的问题?

答案 1 :(得分:0)

今天我偶然发现了与 AutoMapper 10.1.1 相同的问题。 我试图将我的自定义类映射到 DinkToPdf 库中的 MarginSettings 类。 MarginSettings 类有 2 个构造函数:

    public class MarginSettings
    {
        public Unit Unit { get; set; }

        public double? Top { get; set; }

        public double? Bottom { get; set; }

        public double? Left { get; set; }

        public double? Right { get; set; }

        public MarginSettings()
        {
            Unit = Unit.Millimeters;
        }

        public MarginSettings(double top, double right, double bottom, double left) : this()
        {
            Top = top;

            Bottom = bottom;

            Left = left;

            Right = right;
        }

        // the rest of class..
}

看起来由于某种原因,AutoMapper 在尝试将我的自定义类映射到它时,正在调用带有 MarginSettings 类中的参数的构造函数。这样,默认 double 值 (0) 在此构造函数中设置为 double? 属性。

以下是演示此问题的代码:

    class SourceClass
    {
        public double? Top { get; set; }

        public double? Bottom { get; set; }

        public double? Left { get; set; }

        public double? Right { get; set; }
    }

    class ClassWithoutCtor
    {
        public double? Top { get; set; }

        public double? Bottom { get; set; }

        public double? Left { get; set; }

        public double? Right { get; set; }
    }

    // The class similar to one in DinkToPdf library
    class ClassWithCtor
    {
        public string Message1 { get; set; }
        public string Message2 { get; set; }

        public double? Top { get; set; }

        public double? Bottom { get; set; }

        public double? Left { get; set; }

        public double? Right { get; set; }

        public ClassWithCtor() => this.Message1 = "in default ctor";

        public ClassWithCtor(double top, double right, double bottom, double left)
            : this()
        {
            this.Top = new double?(top);
            this.Bottom = new double?(bottom);
            this.Left = new double?(left);
            this.Right = new double?(right);
            Message2 = "in ctor with parameters";
        }
    }

    class AutoMapperTest
    {
        public void TryMapZeros()
        {
            var source = new SourceClass
            {
                Top = 5,
                Bottom = 5
            };

            var mapConfiguration = new MapperConfiguration(
                cfg =>
                {
                    cfg.CreateMap<SourceClass, ClassWithoutCtor>();
                    cfg.CreateMap<SourceClass, ClassWithCtor>();
                }
                );

            var mapper = mapConfiguration.CreateMapper();
            var margin1 = mapper.Map<SourceClass, ClassWithoutCtor>(source);
            var margin2 = mapper.Map<SourceClass, ClassWithCtor>(source);
        }
    }

代码执行后映射的对象是这样的:

margin1
{PdfGeneratorTest.ClassWithoutCtor}
    Bottom: 5
    Left: null
    Right: null
    Top: 5
margin2
{PdfGeneratorTest.ClassWithCtor}
    Bottom: 5
    Left: 0
    Message1: "in default ctor"
    Message2: "in ctor with parameters"
    Right: 0
    Top: 5