列名称中带有前缀的 Dapper 映射
Dapper map with prefix in column name
我有2个classes,顺序和地址如下:
public class Order
{
public string OrderId { get; set; }
public Address ShippingAddress { get; set; }
public Address BillingAddress { get; set; }
}
和
public class Address
{
public string Street { get; set; }
public string Town { get; set; }
public string Zip { get; set; }
}
数据库将订单和地址存储在单个 table 中,如下所示:
CREATE TABLE Orders
(
OrderId NVARCHAR(56) PRIMARY KEY,
BillingStreet NVARCHAR(256),
BillingTown NVARCHAR(256),
BillingZip NVARCHAR(256),
ShippingStreet NVARCHAR(256),
ShippingTown NVARCHAR(256),
ShippingZip NVARCHAR(256)
)
我怎样才能让 dapper 将其映射到订单 class?
我认为 Dapper 不可能做到这一点,因为它将行视为单个对象。如果您更改 table 结构,则有可能:
CREATE TABLE Orders
(
OrderId NVARCHAR(56) PRIMARY KEY,
BillingAddressId INT
ShippingAddressId INT
)
那么您必须将 class 更改为:
public class Order
{
public string OrderId { get; set; }
public int ShippingAddressId {get; set;}
public virtual Address ShippingAddress { get; set; }
public int BillingAddressId {get; set;}
public virtual Address BillingAddress { get; set; }
}
并且只使用多重映射。
另一种选择是使用像 Dapper-FluentMap or Dapper Extensions 这样的 Dapper 扩展,这将帮助您将列映射到 classes。
以下是实现此目的的方法,方法是使查询概括计费和发货列,并使用接受多种类型的 Query
版本,并告诉它在看到名为 [=16= 的列时进行拆分].然后,您只需将 Address
对象分配给 Order
对象上的适当 属性。
connection.Query<Order, Address, Address, Order>(
@"SELECT OrderId,
BillingAddress As Address,
BillingTown As Town,
BillingZip As Zip,
ShippingAddress As Address,
ShippingTown As Town,
ShippingZip As Zip,
FROM Orders",
(o, ba, sa) =>
{
o.BillingAddress = ba;
o.ShippingAddress = sa;
return o;
},
splitOn: "Address");
我有2个classes,顺序和地址如下:
public class Order
{
public string OrderId { get; set; }
public Address ShippingAddress { get; set; }
public Address BillingAddress { get; set; }
}
和
public class Address
{
public string Street { get; set; }
public string Town { get; set; }
public string Zip { get; set; }
}
数据库将订单和地址存储在单个 table 中,如下所示:
CREATE TABLE Orders
(
OrderId NVARCHAR(56) PRIMARY KEY,
BillingStreet NVARCHAR(256),
BillingTown NVARCHAR(256),
BillingZip NVARCHAR(256),
ShippingStreet NVARCHAR(256),
ShippingTown NVARCHAR(256),
ShippingZip NVARCHAR(256)
)
我怎样才能让 dapper 将其映射到订单 class?
我认为 Dapper 不可能做到这一点,因为它将行视为单个对象。如果您更改 table 结构,则有可能:
CREATE TABLE Orders
(
OrderId NVARCHAR(56) PRIMARY KEY,
BillingAddressId INT
ShippingAddressId INT
)
那么您必须将 class 更改为:
public class Order
{
public string OrderId { get; set; }
public int ShippingAddressId {get; set;}
public virtual Address ShippingAddress { get; set; }
public int BillingAddressId {get; set;}
public virtual Address BillingAddress { get; set; }
}
并且只使用多重映射。
另一种选择是使用像 Dapper-FluentMap or Dapper Extensions 这样的 Dapper 扩展,这将帮助您将列映射到 classes。
以下是实现此目的的方法,方法是使查询概括计费和发货列,并使用接受多种类型的 Query
版本,并告诉它在看到名为 [=16= 的列时进行拆分].然后,您只需将 Address
对象分配给 Order
对象上的适当 属性。
connection.Query<Order, Address, Address, Order>(
@"SELECT OrderId,
BillingAddress As Address,
BillingTown As Town,
BillingZip As Zip,
ShippingAddress As Address,
ShippingTown As Town,
ShippingZip As Zip,
FROM Orders",
(o, ba, sa) =>
{
o.BillingAddress = ba;
o.ShippingAddress = sa;
return o;
},
splitOn: "Address");