gpt4 book ai didi

Dapper - 使用带有除 Id 以外的分割点的多重映射

转载 作者:行者123 更新时间:2023-12-04 08:53:22 24 4
gpt4 key购买 nike

我知道这类似于 Correct use of Multimapping in Dapper ,但我认为它略有不同。

我有以下 POCO 结构:

public class Customer
{
public int customerkey { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public List<Invoice> Invoices { get; set; }
public int statekey { get; set; }
public State State { get; set; }

public Customer()
{
this.Invoices = new List<Invoice>();
}
}

public class Invoice
{
public int customerinvoicekey { get; set; }
public int customerkey { get; set; }
public int Number { get; set; }
public string Description { get; set; }
public int Total { get; set; }
public int statuskey { get; set; }
public State State { get; set; }
}

public class State
{
public int statekey { get; set; }
public string Description { get; set; }
}

我正在尝试使用 Dapper 映射它,并且我没有将 Id 用于我的分割点。如果我将 key 加倍,我可以让它工作,但我不确定为什么我必须这样做。

为什么这样做:
const string commandText =
@"SELECT
A.customerkey, A.FirstName, A.LastName, A.EmailAddress, A.statuskey,
C.statuskey, C.Description,
B.customerinvoicekey, B.customerkey, B.Number, B.Description, B.Total, B.statuskey,
D.statuskey, D.Description
FROM Web.TestCustomers2 A
INNER JOIN Web.TestCustomerInvoices2 B ON A.customerkey = B.customerkey
INNER JOIN Web.TestStatus2 C ON A.statuskey = C.statuskey
INNER JOIN Web.TestStatus2 D ON B.statuskey = D.statuskey
ORDER BY A.customerkey";

var customers = new List<Customer>();
Customer currentCustomer = null;
db.Connection.Query<Customer, State, Invoice, State, Customer>(commandText,
(customer, customerstate, invoice, invoicestate) =>
{
if (currentCustomer == null || currentCustomer.customerkey != customer.customerkey)
{
customers.Add(customer);
currentCustomer = customer;
}
invoice.State = invoicestate;
currentCustomer.Invoices.Add(invoice);
currentCustomer.State = customerstate;
return currentCustomer;
}, splitOn: "statuskey,customerinvoicekey,statuskey");

但这不起作用(省略了 A 和 B 中状态键的选择):
const string commandText =
@"SELECT
A.customerkey, A.FirstName, A.LastName, A.EmailAddress,
C.statuskey, C.Description,
B.customerinvoicekey, B.customerkey, B.Number, B.Description, B.Total,
D.statuskey, D.Description
FROM Web.TestCustomers2 A
INNER JOIN Web.TestCustomerInvoices2 B ON A.customerkey = B.customerkey
INNER JOIN Web.TestStatus2 C ON A.statuskey = C.statuskey
INNER JOIN Web.TestStatus2 D ON B.statuskey = D.statuskey
ORDER BY A.customerkey";

var customers = new List<Customer>();
Customer currentCustomer = null;
db.Connection.Query<Customer, State, Invoice, State, Customer>(commandText,
(customer, customerstate, invoice, invoicestate) =>
{
if (currentCustomer == null || currentCustomer.customerkey != customer.customerkey)
{
customers.Add(customer);
currentCustomer = customer;
}
invoice.State = invoicestate;
currentCustomer.Invoices.Add(invoice);
currentCustomer.State = customerstate;
return currentCustomer;
}, splitOn: "statuskey,customerinvoicekey,statuskey");

最佳答案

究竟在哪里customerinvoicekey来自(哪里?

您的模型声称表的键是 public int customerkey { get; set; } .

如果在模型中没有的列上拆分,则多重映射函数的行为是未定义的。

关于Dapper - 使用带有除 Id 以外的分割点的多重映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7837390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com