c# - 映射超过 7 种类型的 Dapper Multimapping,System.ArgumentException

标签 c# dapper

我使用 dapper,我有超过 7 种类型的映射。

我知道在 dapper 中存在一个方法 IEnumerable<TReturn> Query<TReturn>(this IDbConnection cnn, string sql, Type[] types, Func<object[], TReturn> map, [Dynamic] dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null)

并且有回复here有很好的例子,但我无法解决我的问题。

假设有这张表

Table Poi(
    Id,
    Date,
    LineId FK Line table,
    TrackId FK Track table,
    State_Id FK State table,
    Type_Id FK Type table,
    Category_Id FK Category table,
    Processing_Id FK Processing table,
    Vehicle_Id FK Vehicle table,
    SystemId FK System table,
    UnitId FK Unit table,
    Speed)

一个 Poco 喜欢:

class Poi
{
        public long Id { get; set; }
        public DateTime Date { get; set; }
        public float Speed { get; set; }        
        public  State State { get; set; }
        public  Type PoiType { get; set; }
        public  Category Category { get; set; }
        public  Processing Processing { get; set; }
        public  Vehicles Vehicle { get; set; }
        public  Line Line { get; set; }
        public  Track Track { get; set; }
        public  System System { get; set; }
        public  Unit Unit { get; set; }    
}

假设有这个sql查询

select select poi.*, ps.Id, ps.Name, pt.name,pt.id,pc.id,pc.name,pc.issystem,processing.id,processing.name,processing.date, v.name,v.id,
                        t.id,t.name,t.code, ds.name, ds.id, du.id, du.name from Poi poi
left join State ps on poi.State_Id = ps.Id
left join Type pt on poi.PoiType_Id = pt.Id
left join Category pc on poi.Category_Id = pc.Id
left join Processing processing on poi.Processing_Id = processing.Id
left join Vehicles v on poi.Vehicle_Id = v.Id
left join Line l on poi.LineId = l.Id
left join Track t on poi.TrackId = t.Id
left join DiagSystem ds on poi.SystemId = ds.Id
left join DiagUnit du on poi.UnitId = du.Id

现在,根据附加的回复,我用这个参数调用方法

connection.Query<Poi>(sql,
   new[]
   {
      typeof(Poi),
      typeof(State),
      typeof(Type),
      typeof(Category),
      typeof(Processing),
      typeof(Vehicles),
      typeof(Line),
      typeof(Track),
      typeof(System),
      typeof(Unit)
   },
   objects =>
   {
       Poi poi = objects[0] as Poi;
       State ps = objects[1] as State;
       Type pt = objects[2] as Type;
       Category pc = objects[3] as Category;
       Processing processing = objects[4] as Processing;
       Vehicles v = objects[5] as Vehicles;
       Line l = objects[6] as Line;
       Track t = objects[7] as Track;
       System ds = objects[8] as System;
       Unit du = objects[9] as Unit;

       poi.State = ps;
       poi.Type = pt;
       poi.Category = pc;
       poi.Processing = processing;
       poi.Vehicle = v;
       poi.Line = l;
       poi.Track = t;
       poi.System = ds;
       poi.Unit = du;  

     return poi;
   },
   new { Value = value }
   ,splitOn: "State_Id,PoiType_Id,Category_Id,Processing_Id,Vehicle_Id,LineId,TrackId,SystemId,UnitId"
).ToList();

但我继续收到关于多映射的相同错误:

'System.ArgumentException' in Dapper.dll

Additional information: When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id

你觉得是spliton参数有误吗??我不明白!

最佳答案

代替:

 select poi.*, ps.Id, ps.Name, pt.name,pt.id
 splitOn: "State_Id,PoiType_Id"

试试这个:

 select poi.*, ps.Id, ps.Name, pt.id, pt.name
 splitOn: "Id,Id"

“State_Id”是Poi中的外键。尝试拆分“Id”。另外,确保主键在选择中排在第一位。 Dapper 将第一组列放在第一个对象中,将第二组列(从第一次拆分开始)放在第二个对象中,依此类推。

Dapper 默认根据“Id”拆分,因此在这些更改后您可以使用默认值 splitOn。

关于c# - 映射超过 7 种类型的 Dapper Multimapping,System.ArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36941226/

相关文章:

C# - 免费离线语音识别库 (SDK)

c# - 如何捕获通过 Ado.Net 发送的所有 SQL

c# - Dapper 如何通过 DynamicParameter.Get 处理 DBNull

c# - 为什么在不使用 async/await 的情况下使用 Dapper QueryAsync<T> 时抛出 TaskCanceledException?

c# - 如何在 DataContext 略有不同的情况下重用 UserControl?

c# - Linq to SQL 中的 DAO 工厂

c# - 如何将新的 MediaType 添加到 ASP.Net WebApi 使用的 MediaTypeFormatters?

c# - 如何将函数参数列表转换为元组

c# - Dapper 使用列表插入数据库

c# - 如何解决 Dapper System.Data.DataException HResult=0x80131501 InvalidCastException Invalid cast from 'System.String' to 'System.Uri'