WHERE 子句中的 Dapper 和 DateTime 导致多映射空值
Dapper and DateTime in WHERE clause causes multi-mapped nulls
我终于能够构建多映射查询和 return 有意义的数据。此查询 return 编辑了一个自定义对象的列表,该对象本身由一些其他对象组成。但是这个查询只用了一个参数。
当我通过添加第二个参数 DateTime 修改此查询时,两个聚合对象(Part 和 Color)为空。但是,当我在分析器中捕获 SQL 并在 SQL 服务器中捕获 运行 时,所有数据都在那里!
如何更好地处理 where 子句中的 DateTime 参数?显然这是导致问题的原因。
后面的代码适用于注释掉的 where 子句,但不适用于现有的 where 子句。
public IList<PartReceipt> GetReceiptHistory(ListItem supplier, DateTime dateReceived)
{
const string sql =
@"SELECT r.id, r.Quantity, r.UnitCost, r.DateReceived,
s.Id , s.Description,
p.Id, p.Number, p.Description, p.StockClass,
mp.Id , mp.Number, mp.ManufacturerId, mp.Description,
m.Id , m.Description, m.ShortDescription,
c.Id, c.Description,
pc.Id, pc.Name, pc.Description
FROM PartReceipt r
INNER JOIN Supplier s ON r.SupplierId = s.Id
INNER JOIN Part p on r.PartId = p.Id
INNER JOIN ManufacturerPart mp ON p.ManufacturerPartId=mp.Id
INNER JOIN Manufacturer m ON mp.ManufacturerId = m.Id
LEFT JOIN Color c ON p.ColorId=c.Id
LEFT JOIN ProductCategory pc ON p.ProductCategoryId=pc.Id
WHERE s.Id=@supplierId AND r.DateReceived = @dateReceived";
// WHERE s.Id=@supplierId";
IList<PartReceipt> reportData;
using (DbConnection connection = ConnectionFactory.GetOpenConnection())
{
reportData = connection.Query<PartReceipt>(sql,
new
{
supplierId = supplier.Id,
dateReceived
}).ToList();
}
return reportData;
}
支持类的是:
public class PartReceipt
{
public int Id { get; set; }
public Supplier Supplier { get; set; }
public Part Part { get; set; }
public DateTime DateReceived { get; set; }
public Decimal UnitCost { get; set; }
public int Quantity { get; set; }
}
public class Part
{
public Color Color { get; set; }
public string Description { get; set; }
public int Id { get; set; }
public ManufacturerPart ManufacturerPart { get; set; }
public string Number { get; set; }
public string PicturePath { get; set; }
public ProductCategory ProductCategory { get; set; }
public string StockClass { get; set; }
}
public class ManufacturerPart
{
public string Description { get; set; }
public int Id { get; set; }
public int ManufacturerId { get; set; }
public string Number { get; set; }
public Manufacturer Parent { get; set; }
}
public class Manufacturer
{
public string Description { get; set; }
public int Id { get; set; }
public string ShortDescription { get; set; }
}
public class ProductCategory
{
public string Description { get; set; }
public int Id { get; set; }
public string Name { get; set; }
}
public class Color
{
public string Description { get; set; }
public int Id { get; set; }
}
对不起,我在发布这个问题之前没有更加小心。我没有正确构造多映射查询。当我这样做时,它的工作。
public IList<PartReceipt> GetReceiptPart(ListItem supplier, DateTime dateReceived)
{
const string sql =
@"SELECT r.id, r.Quantity, r.UnitCost, r.DateReceived,
s.Id , s.Description,
p.Id, p.Number, p.Description, p.StockClass,
mp.Id , mp.Number, mp.ManufacturerId, mp.Description,
m.Id , m.Description, m.ShortDescription,
c.Id, c.Description,
pc.Id, pc.Name, pc.Description
FROM PartReceipt r
INNER JOIN Supplier s ON r.SupplierId = s.Id
INNER JOIN Part p on r.PartId = p.Id
INNER JOIN ManufacturerPart mp ON p.ManufacturerPartId=mp.Id
INNER JOIN Manufacturer m ON mp.ManufacturerId = m.Id
LEFT JOIN Color c ON p.ColorId=c.Id
LEFT JOIN ProductCategory pc ON p.ProductCategoryId=pc.Id
WHERE s.Id=@supplierId AND r.DateReceived = @dateReceived";
IList<PartReceipt> reportData;
using (DbConnection connection = ConnectionFactory.GetOpenConnection())
{
reportData =
connection
.Query
<PartReceipt, Supplier, Part, ManufacturerPart, Manufacturer, Color, ProductCategory, PartReceipt>(
sql,
(receipt, supp, part, mfgPart, mfg, color, productCategory) =>
{
receipt.Supplier = supp;
receipt.Part = part;
receipt.Part.ManufacturerPart = mfgPart;
receipt.Part.ManufacturerPart.Parent = mfg;
receipt.Part.Color = color;
receipt.Part.ProductCategory = productCategory;
return receipt;
}, new { supplierId = supplier.Id, dateReceived })
.ToList();
}
return reportData;
}
我终于能够构建多映射查询和 return 有意义的数据。此查询 return 编辑了一个自定义对象的列表,该对象本身由一些其他对象组成。但是这个查询只用了一个参数。
当我通过添加第二个参数 DateTime 修改此查询时,两个聚合对象(Part 和 Color)为空。但是,当我在分析器中捕获 SQL 并在 SQL 服务器中捕获 运行 时,所有数据都在那里!
如何更好地处理 where 子句中的 DateTime 参数?显然这是导致问题的原因。
后面的代码适用于注释掉的 where 子句,但不适用于现有的 where 子句。
public IList<PartReceipt> GetReceiptHistory(ListItem supplier, DateTime dateReceived)
{
const string sql =
@"SELECT r.id, r.Quantity, r.UnitCost, r.DateReceived,
s.Id , s.Description,
p.Id, p.Number, p.Description, p.StockClass,
mp.Id , mp.Number, mp.ManufacturerId, mp.Description,
m.Id , m.Description, m.ShortDescription,
c.Id, c.Description,
pc.Id, pc.Name, pc.Description
FROM PartReceipt r
INNER JOIN Supplier s ON r.SupplierId = s.Id
INNER JOIN Part p on r.PartId = p.Id
INNER JOIN ManufacturerPart mp ON p.ManufacturerPartId=mp.Id
INNER JOIN Manufacturer m ON mp.ManufacturerId = m.Id
LEFT JOIN Color c ON p.ColorId=c.Id
LEFT JOIN ProductCategory pc ON p.ProductCategoryId=pc.Id
WHERE s.Id=@supplierId AND r.DateReceived = @dateReceived";
// WHERE s.Id=@supplierId";
IList<PartReceipt> reportData;
using (DbConnection connection = ConnectionFactory.GetOpenConnection())
{
reportData = connection.Query<PartReceipt>(sql,
new
{
supplierId = supplier.Id,
dateReceived
}).ToList();
}
return reportData;
}
支持类的是:
public class PartReceipt
{
public int Id { get; set; }
public Supplier Supplier { get; set; }
public Part Part { get; set; }
public DateTime DateReceived { get; set; }
public Decimal UnitCost { get; set; }
public int Quantity { get; set; }
}
public class Part
{
public Color Color { get; set; }
public string Description { get; set; }
public int Id { get; set; }
public ManufacturerPart ManufacturerPart { get; set; }
public string Number { get; set; }
public string PicturePath { get; set; }
public ProductCategory ProductCategory { get; set; }
public string StockClass { get; set; }
}
public class ManufacturerPart
{
public string Description { get; set; }
public int Id { get; set; }
public int ManufacturerId { get; set; }
public string Number { get; set; }
public Manufacturer Parent { get; set; }
}
public class Manufacturer
{
public string Description { get; set; }
public int Id { get; set; }
public string ShortDescription { get; set; }
}
public class ProductCategory
{
public string Description { get; set; }
public int Id { get; set; }
public string Name { get; set; }
}
public class Color
{
public string Description { get; set; }
public int Id { get; set; }
}
对不起,我在发布这个问题之前没有更加小心。我没有正确构造多映射查询。当我这样做时,它的工作。
public IList<PartReceipt> GetReceiptPart(ListItem supplier, DateTime dateReceived)
{
const string sql =
@"SELECT r.id, r.Quantity, r.UnitCost, r.DateReceived,
s.Id , s.Description,
p.Id, p.Number, p.Description, p.StockClass,
mp.Id , mp.Number, mp.ManufacturerId, mp.Description,
m.Id , m.Description, m.ShortDescription,
c.Id, c.Description,
pc.Id, pc.Name, pc.Description
FROM PartReceipt r
INNER JOIN Supplier s ON r.SupplierId = s.Id
INNER JOIN Part p on r.PartId = p.Id
INNER JOIN ManufacturerPart mp ON p.ManufacturerPartId=mp.Id
INNER JOIN Manufacturer m ON mp.ManufacturerId = m.Id
LEFT JOIN Color c ON p.ColorId=c.Id
LEFT JOIN ProductCategory pc ON p.ProductCategoryId=pc.Id
WHERE s.Id=@supplierId AND r.DateReceived = @dateReceived";
IList<PartReceipt> reportData;
using (DbConnection connection = ConnectionFactory.GetOpenConnection())
{
reportData =
connection
.Query
<PartReceipt, Supplier, Part, ManufacturerPart, Manufacturer, Color, ProductCategory, PartReceipt>(
sql,
(receipt, supp, part, mfgPart, mfg, color, productCategory) =>
{
receipt.Supplier = supp;
receipt.Part = part;
receipt.Part.ManufacturerPart = mfgPart;
receipt.Part.ManufacturerPart.Parent = mfg;
receipt.Part.Color = color;
receipt.Part.ProductCategory = productCategory;
return receipt;
}, new { supplierId = supplier.Id, dateReceived })
.ToList();
}
return reportData;
}