Automapper 8忽略属性不适用于子代后代

时间:2019-04-28 01:38:05

标签: c# asp.net-core automapper

我在asp.net核心项目中使用AutoMapper 8.1.0。我有一个Automapper映射,该映射无法正常运行。我复制了配置,以便您可以自己对其进行测试。因此,我有一个ExpenseReport带有一个ExpenseReportItem集合,而这个有另一个集合。映射后,我必须保留eTaxCollection的数据,但是在过程中会丢失它们。

所以问题是为什么在调用_mapper.Map(vmodel,model)之后,eTaxCollections的值会丢失,如何保存它们?

ignore属性不起作用。我还尝试了UseDestinationValue()。我花了2天的时间试图弄清楚,但我已经筋疲力尽了。

    public void WeatherForecasts()
            {
                int[] excludeTaxes = new int[] { 2 };
                var vmodel = new ExpenseReportCreateEditModel();
                vmodel.Expenses.Add(new ExpenseReportItemModel()
                {
                    ExcludeTaxIds = excludeTaxes,
                    Total = 12,
                    Id = 1
                });

                // fetch from bd
                var model = new ExpenseReport();
                // values will be lost after _mapper.Map...
                var eTaxCollections = new HashSet<ExcludeExpenseReportItemTax>();
                eTaxCollections.Add(new ExcludeExpenseReportItemTax()
                {
                    TaxId = 1,
                    ExpenseReportItemId = 1
                });

                model.Items.Add(new ExpenseReportItem()
                {
                    ExcludeTaxes = eTaxCollections,
                    ExpenseReportId = 1,
                    Id = 9
                });

                _mapper.Map(vmodel, model);
            }

public class ExpenseReportCreateEditModelProfile : Profile
        {
            public ExpenseReportCreateEditModelProfile()
            {
                CreateMap<ExpenseReportCreateEditModel, ExpenseReport>()
                    .ForMember(d => d.Items, s => s.MapFrom(m => m.Expenses));
            }
        }

        public class ExpenseReportItemModelProfile : Profile
        {
            public ExpenseReportItemModelProfile()
            {
                CreateMap<ExpenseReportItemModel, ExpenseReportItem>()
                    .ForMember(d => d.ExcludeTaxes, s => s.Ignore()); // <<<==== data are lost
            }
        }
        public class ExpenseReportCreateEditModel
        {
            public int Id { get; set; }
            public ICollection<ExpenseReportItemModel> Expenses { get; set; }
            public ExpenseReportCreateEditModel()
            {
                Expenses = new HashSet<ExpenseReportItemModel>();
            }
        }

        public class ExpenseReportItemModel
        {
            public int Id { get; set; }
            public ICollection<int> ExcludeTaxIds { get; set; }
            public decimal Total { get; set; }

            public ExpenseReportItemModel()
            {
                ExcludeTaxIds = new HashSet<int>();
            }
        }

        public class ExpenseReport
        {
            public int Id { get; set; }
            public virtual ICollection<ExpenseReportItem> Items { get; set; }
            public ExpenseReport()
            {
                Items = new HashSet<ExpenseReportItem>();
            }
        }

        public class ExpenseReportItem 
        {
            public int Id { get; set; }
            public int ExpenseReportId { get; set; }
            public virtual ICollection<ExcludeExpenseReportItemTax> ExcludeTaxes { get; set; }
            public ExpenseReportItem()
            {
                ExcludeTaxes = new HashSet<ExcludeExpenseReportItemTax>();
            }
        }

        public class ExcludeExpenseReportItemTax
        {
            public int ExpenseReportItemId { get; set; }
            public virtual ExpenseReportItem ExpenseReportItem { get; set; }
            public int TaxId { get; set; }
        }

谢谢您的帮助

编辑


我执行了执行计划,也许这就是问题所在

$typeMapDestination = ($dest ?? .New WebApplication1.Controllers.SampleDataController+ExpenseReportItem());

这是我丢失值的唯一方法。 我现在必须找到解决方法

这是完整的执行计划:

.If ($src == null) {
    .Default(WebApplication1.Controllers.SampleDataController+ExpenseReportItem)
} .Else {
    .Block() {
        $typeMapDestination = ($dest ?? .New WebApplication1.Controllers.SampleDataController+ExpenseReportItem());
        .Try {
            .Block(System.Int32 $resolvedValue) {
                .Block() {
                    $resolvedValue = .If (
                        $src == null || False
                    ) {
                        .Default(System.Int32)
                    } .Else {
                        $src.Id
                    };
                    $typeMapDestination.Id = $resolvedValue
                }
            }
        } .Catch (System.Exception $ex) {
            .Block() {
                .Throw .New AutoMapper.AutoMapperMappingException(
                    "Error mapping types.",
                    $ex,
                    .Constant<AutoMapper.TypePair>(AutoMapper.TypePair),
                    .Constant<AutoMapper.TypeMap>(AutoMapper.TypeMap),
                    .Constant<AutoMapper.PropertyMap>(AutoMapper.PropertyMap));
                .Default(System.Int32)
            }
        };
        $typeMapDestination
    }
}

0 个答案:

没有答案