我有两张桌子。带有 CustomerID、InvoiceDate、Value、InvoiceTypeID 列的发票(CustomerID 和 InvoiceDate 组成一个复合键)和带有 InvoiceTypeID 和 InvoiceTypeName 列的 InvoiceType。
我知道我可以创建我的对象,例如:
public class Invoice
{
public virtual int CustomerID { get; set; }
public virtual DateTime InvoiceDate { get; set; }
public virtual decimal Value { get; set; }
public virtual InvoiceType InvoiceType { get; set; }
}
public class InvoiceType
{
public virtual InvoiceTypeID { get; set; }
public virtual InvoiceTypeName { get; set; }
}
所以生成的 sql 看起来像:
SELECT CustomerID, InvoiceDate, Value, InvoiceTypeID FROM Invoice WHERE CustomerID = x AND InvoiceDate = y
SELECT InvoiceTypeID, InvoiceTypeName FROM InvoiceType WHERE InvoiceTypeID = z
但是,我宁愿执行两个选择查询来检索数据。我还想避免将子对象用于简单的查找列表。所以我的对象看起来像:
public class Invoice
{
public virtual int CustomerID { get; set; }
public virtual DateTime InvoiceDate { get; set; }
public virtual decimal Value { get; set; }
public virtual InvoiceTypeID { get; set; }
public virtual InvoiceTypeName { get; set; }
}
我的 sql 看起来像:
SELECT CustomerID, InvoiceDate, Value, InvoiceTypeID
FROM Invoice INNER JOIN InvoiceType ON Invoice.InvoiceTypeID = InvoiceType.InvoiceTypeID
WHERE CustomerID = x AND InvoiceDate = y
我的问题是如何为此创建映射?
我尝试过使用 join 但这尝试使用 CustomerID 和 InvoiceDate 加入,我是否遗漏了一些明显的东西?
谢谢