我有一个 C# .NET Core 2.0 Web API 项目(作为微服务实现)。
我在我的存储库层中使用 NuGet 包“Dapper”和“FastMember”(最新版本)来尝试IEnumerable<T>
通过 FastMember 提供的 ObjectReader 将 an 转换为 DataTable,这样我就可以利用 Dapper 的内置 ICustomQueryParameter SqlMapper 来传递我在 SQL Server 中使用的存储过程的表值参数。
当我执行和调试我的代码时,它会在 FastMember ObjectReader 尝试加载到 DataTable: 的行上引发异常table.Load(reader);
。
这是我的 Dapper 包装器/扩展方法,其中包含有问题的行:
public static class DapperExtensions
{
public static SqlMapper.ICustomQueryParameter AsTvp<T>(this
IEnumerable<T> enumerable, string typeName) where T : class
{
var table = new DataTable();
var members = typeof(T).GetProperties().Select(p => p.Name).ToArray();
using (var reader = ObjectReader.Create(enumerable.ToList(), members))
{
table.Load(reader);
}
return table.AsTableValuedParameter(typeName);
}
}
这是我使用上述扩展方法的 Dapper 调用:
using (var dbConnection = new SqlConnection(_connectionString))
{
var result = dbConnection.Query("[PurchaseOrders].[Add] @tvpNewPurchaseOrderItems, @StartDate", new
{
tvpNewPurchaseOrderItems = purchaseOrderCreationRequest.PurchaseOrderItems.AsTvp("NewPurchaseOrderType"),
StartDate = purchaseOrderCreationRequest.StartDate
});
}
这是我抛出的 C# 异常的堆栈跟踪:
at System.Data.Common.DbDataReader.GetSchemaTable()
at System.Data.ProviderBase.SchemaMapping..ctor(DataAdapter adapter, DataSet dataset, DataTable datatable, DataReaderContainer dataReader, Boolean keyInfo, SchemaType schemaType, String sourceTableName, Boolean gettingData, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.FillMappingInternal(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 schemaCount, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.FillMapping(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 schemaCount, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)
at System.Data.DataTable.Load(IDataReader reader)
at PurchaseOrderCreationRepositories.Extensions.DapperExtensions.AsTvp[T](IEnumerable`1 enumerable, String typeName) in C:\Projects-Git\Enterprise\PurchaseOrderCreationService\PurchaseOrderCreationRepositories\Extensions\DapperExtensions.cs:line 16
at PurchaseOrderCreationRepositories.PurchaseOrderRepository.Add(IPurchaseOrderCreationRequest purchaseOrderCreationRequest) in C:\Projects-Git\Enterprise\PurchaseOrderCreationService\PurchaseOrderCreationRepositories\PurchaseOrderRepository.cs:line 22
at PurchaseOrderCreationServices.PurchaseOrderService.Add(IPurchaseOrderCreationRequest purchaseOrderCreationRequest) in C:\Projects-Git\Enterprise\PurchaseOrderCreationService\PurchaseOrderCreationServices\PurchaseOrderService.cs:line 18
at PurchaseOrderCreationServiceApi.Controllers.PurchaseOrderCreationController.Add(PurchaseOrderCreationRequest purchaseOrderCreationRequest) in C:\Projects-Git\Enterprise\PurchaseOrderCreationService\PurchaseOrderCreationServiceApi\Controllers\PurchaseOrderCreationController.cs:line 25
异常消息是:Specified method is not supported.
有人可以帮我解决这个错误吗?.NET Core 2.0 不支持这种方法对我来说没有意义。我已经通过 SqlBulkCopy 在其他 .NET Core 2.0 应用程序中成功使用了 FastMember。但这是我第一次尝试将它与 Dapper 一起使用。
任何帮助深表感谢!提前致谢。