【发布时间】:2020-07-02 09:26:41
【问题描述】:
我有一个使用 AutoMapper(当前版本 9)的大型项目
我尝试更新到 V10,但我的代码崩溃了,我不明白我需要更改什么来解决它
我有多个使用基类的对象,这些对象继承了这个基类的通用功能。
这有3个主要原因,
- 所有对象都有一个 .ReadRecord 方法,允许我实例化记录并从 SQL 读取数据,
- 所有对象都有 CreatedBy、CreateDate、AmendedBy、AmendedDate,当然还有 ID 属性
- 所有对象都有一个与主记录相同的 OriginalRecord 属性,因此我可以比较主记录中的更改,因此我只将这些属性保存到数据库并更新审核日志
该过程在我填充主对象和 OriginalRecord 属性的初始读取时失败
这在 V9 上工作得很好,但在 v10 上却不行
// Return the record for the specified ID
internal bool _ReadRecord<TENTITY>(int ID) where TENTITY : class, new()
{
_OriginalRecord = new TENTITY();
if (ID > 0)
{
TENTITY tmpRecord = BTPUtility.sqlDbAccessObject.ReadRecord<TENTITY>(ID);
if (tmpRecord != null)
{
var config = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.AllowNullCollections = true;
cfg.CreateMap<TENTITY, TENTITY>();
});
var mapper = config.CreateMapper();
mapper.Map(tmpRecord, this);
_OriginalRecord = new TENTITY();
mapper.Map(tmpRecord, _OriginalRecord);
this.isRead = true;
return true;
}
}
return false;
}
失败发生在 mapper.Map(tmpRecord, _OriginalRecord);
然而在读取之前使用的代码是
vwQuoteHeader gridRecord = gvQuotes.GetRow(selectedRows[0]) as BTP.Business.Model.Quotes.vwQuoteHeader;
// Ensure the record type is the base record not the view
var config = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.AllowNullCollections = true;
cfg.CreateMap<vwQuoteHeader, QuoteHeader>();
});
var mapper = config.CreateMapper();
mapper.Map(gridRecord, SelectedRecord);
SelectedRecord.ReadRecord();
在它操作SelectedRecord.ReadRecord()之前工作正常
我得到的错误是
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: '无法从中推断方法'AutoMapper.IMapper.Map
(TSource, System.Action >)'的类型参数用法。尝试明确指定类型参数。'
我尝试按照文档进行操作,但不明白出了什么问题!
任何指针将不胜感激
【问题讨论】:
标签: c# winforms automapper automapper-10