gpt4 book ai didi

c# - AutoMapper 的 AssertConfigurationIsValid 仅在第一次加载时失败?

转载 作者:行者123 更新时间:2023-11-30 21:07:41 25 4
gpt4 key购买 nike

在 X/WebSiteMVC3/Core/DependencyResolution/XProfile.cs 中,我有一个看起来有点像这样的现有映射:

 CreateMap<DomainObjects.Entities.Thing1, Models.Thing1>();
CreateMap<Models.Thing1, DomainObjects.Entities.Thing1>()
.ForMember(a => a.Thing2, opt => opt.Ignore())
.ForMember(a => a.ModifiedBy, opt => opt.Ignore())
.ForMember(a => a.ModifiedDate, opt => opt.Ignore())
.ForMember(a => a.CreatedBy, opt => opt.Ignore())
.ForMember(a => a.CreatedDate, opt => opt.Ignore());

我需要为它的子对象添加一个映射,所以我把这个:

 CreateMap<DomainObjects.Entities.Thing2, Models.Thing2>();
CreateMap<Models.Thing2, DomainObjects.Entities.Thing2>()
.ForMember(a => a.ModifiedBy, opt => opt.Ignore())
.ForMember(a => a.ModifiedDate, opt => opt.Ignore())
.ForMember(a => a.CreatedBy, opt => opt.Ignore())
.ForMember(a => a.CreatedDate, opt => opt.Ignore());

它有效,除了在第一页加载时,我得到了这个:

Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type

Thing2 -> Thing2 (Destination member list)

X.X.WebSiteMVC3.Models.Thing2 -> X.X.DomainObjects.Entities.Thing2 (Destination member list)

Thing1

堆栈跟踪:

AutoMapper.ConfigurationStore.AssertConfigurationIsValid(IEnumerable`1 typeMaps) +684 AutoMapper.ConfigurationStore.AssertConfigurationIsValid() +12 AutoMapper.Mapper.AssertConfigurationIsValid() +23 X.X.WebSiteMVC3.Core.DependencyResolution.AutomapperRegistry.Configure() in C:\Source\X.X.WebSiteMVC3\Core\DependencyResolution\AutomapperRegistry.cs:13 X.X.WebSiteMVC3.MvcApplication.Application_Start() in C:\Source\X.X.WebSiteMVC3\Global.asax.cs:96

但是在每一次 其他后续加载中,它都按预期工作!?

那么……当 Thing2 的实现与 Thing1 的实现相匹配时,为什么 Thing2 会失败(它始终有效)?为什么在 Thing2 的错误中提到了 Thing1(我觉得这就是原因,但如果我能在这个闲置的星期四上午 10 点看到它,该死的)?

Muchos Danke!

最佳答案

最后,这是由 Thing2 上的交叉引用返回到 Thing1 引起的...所以我不得不这样做...

 CreateMap<DomainObjects.Entities.Thing2, Models.Thing2>();
CreateMap<Models.Thing2, DomainObjects.Entities.Thing2>()
!-> .ForMember(a => a.Thing1, opt => opt.Ignore())
.ForMember(a => a.ModifiedBy, opt => opt.Ignore())
.ForMember(a => a.ModifiedDate, opt => opt.Ignore())
.ForMember(a => a.CreatedBy, opt => opt.Ignore())
.ForMember(a => a.CreatedDate, opt => opt.Ignore());

真正让我感到奇怪的是,我收到的错误消息(“找到未映射的成员...”)没有出现在 Google 上!?通常发生这种情况时,我已经设法做了一些非常奇怪/奇怪的事情,因此我很快就会在这里提问。在这种情况下,问题有点微不足道。

所以...对于可能通过 Google 到达这里的其他人:这可能与您的模型本身有关,而不是与 AutoMapper 有一些奇怪之处。尽管我仍然不知道为什么映射在第二次传递时“有效”!?这太奇怪了!

关于c# - AutoMapper 的 AssertConfigurationIsValid 仅在第一次加载时失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10219935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com