我一直在玩这个,因为它看起来感觉很像记录的帖子/用户示例,但它略有不同,并不适合我.
假设以下简化设置(联系人有多个电话号码):
public class Contact
{
public int ContactID { get; set; }
public string ContactName { get; set; }
public IEnumerable<Phone> Phones { get; set; }
}
public class Phone
{
public int PhoneId { get; set; }
public int ContactID { get; set; } // foreign key
public string Number { get; set; }
public string Type { get; set; }
public bool IsActive { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我希望最终能够返回一个与多个Phone对象联系的东西.这样,如果我有2个联系人,每个联系人有2个电话,我的SQL将返回一个连接,作为结果集共4行.然后Dapper将弹出2个接触对象,每个接触对象有两部手机.
这是存储过程中的SQL:
SELECT *
FROM Contacts
LEFT …Run Code Online (Sandbox Code Playgroud) 我已经编写了这个代码来实现一对多的关系,但它不起作用:
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
IEnumerable<Store> stores = connection.Query<Store, IEnumerable<Employee>, Store>
(@"Select Stores.Id as StoreId, Stores.Name,
Employees.Id as EmployeeId, Employees.FirstName,
Employees.LastName, Employees.StoreId
from Store Stores
INNER JOIN Employee Employees ON Stores.Id = Employees.StoreId",
(a, s) => { a.Employees = s; return a; },
splitOn: "EmployeeId");
foreach (var store in stores)
{
Console.WriteLine(store.Name);
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以发现错误吗?
编辑:
这些是我的实体:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public …Run Code Online (Sandbox Code Playgroud)