Dapper - использование множественного сопоставления с точками разделения, отличными от Id

Я знаю, что это похоже на Правильное использование Multimapping в 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 Invoices { get; set; }
    public int statekey { get; set; }
    public State State { get; set; }

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

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 для точек разделения. Я могу заставить его работать, если удвою ключи, но я не уверен, зачем мне это делать.

Почему это работает:

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 currentCustomer = null;
        db.Connection.Query(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 currentCustomer = null;
        db.Connection.Query(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");

8
задан Community 23 May 2017 в 11:51
поделиться