我有这个:
public class object_a
{
public int ta_Id { get; set; }
public string ta_Label { get; set; }
public IEnumerable<table_c> SomeName { get; set; }
}
public class table_b
{
public int Id {get;set;}
public int SomeId {get;set;}
public int FK_A {get;set;}
}
public class table_c
{
public int Id {get;set;}
public int Max {get;set;}
public string Label {get;set;}
public int FK_A {get;set;}
}
使用 Dapper 我检索object_a
具有任意数量的子对象的列表table_c
using (System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(_con))
{
sqlConnection.Open();
var sql = string.Format(
@"
select ta.Id as ta_Id, ta.Label as ta_label, splitLimit = '', tc.Id, tc.Max, tc.Label
from table_a as ta
left join table_b as tb
on tb.FK_A = ta.Id
left join table_c as tc
on tc.FK_A = ta.Id
where tb.SomeId = SomeInt);
var items = sqlConnection.Query<object_a, IEnumerable<table_c>, object_a>(sql, (a, c) => { a.c = table_c; return a; }, splitOn: "splitLimit");
return items;
}
从控制器调用 Dapper 代码时,我收到此错误:
需要一个无参数的默认构造函数来实现简洁的物化
我也试过这个:
var items = sqlConnection.Query<object_a,IEnumerable<table_c>, object_a>(sql, (a, c) =>
{
if (c!= null)
{
a.SomeName = c;
}
return a;
}, splitOn: "splitLimit");
我认为这可能与,IEnumerable<table_c>
但我不明白我在这里做错了什么。我已经阅读了 SO 建议的相关问题,但我不“明白”它。
我想知道我做错了什么以及正确的代码是什么。谢谢!