【问题标题】:map nullable int to nullable int + automapper将可为空的 int 映射到可为空的 int + automapper
【发布时间】: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());

而在 PJ 中的问题是:

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

问候

【问题讨论】:

  • 您的代码应该成功地将 null 映射到 null。我将从检查FORMULAIRE_ID 的所有写入用法开始。也许它的值在属性映射后被改变(在另一个属性设置器或其他东西中?)。
  • 不,无论如何都不会改变!!!

标签: c# entity-framework mapping automapper


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    今天我偶然发现了与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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多