使用 Dapper.net 的多重映射查询出现意外行为
我刚刚开始查看 Dapper.net,并且刚刚尝试了一些不同的查询,其中之一产生了我意想不到的奇怪结果。
我有 2 张桌子 - 照片
和PhotoCategories
,其中与 CategoryID
相关
照片表
PhotoId (PK - int)
CategoryId (FK - smallint)
UserId (int)
PhotoCategories 表
CategoryId (PK - smallint)
CategoryName (nvarchar(50))
我的 2 个课程:
public class Photo
{
public int PhotoId { get; set; }
public short CategoryId { get; set; }
public int UserId { get; set; }
public PhotoCategory PhotoCategory { get; set; }
}
public class PhotoCategory
{
public short CategoryId { get; set; }
public string CategoryName { get; set; }
{
我想使用多重映射返回 Photo
的实例,以及相关 PhotoCategory
的填充实例。
var sql = @"select p.*, c.* from Photos p inner
join PhotoCategories c
on p.CategoryID = c.CategoryID where p.PhotoID = @pid";
cn.Open();
var myPhoto = cn.Query<Photo, PhotoCategory, Photo>(sql,
(photo, photoCategory) => { photo.PhotoCategory = photoCategory;
return photo; },
new { pid = photoID }, null, true, splitOn: "CategoryID").Single();
执行此操作时,并非所有属性都会被填充(尽管数据库表和我的对象中的名称相同)。
我注意到,如果我不
“选择 p.* 等”在我的 SQL
中,
。
我想从查询中返回 EXCLUDING
p.CategoryId
,然后所有内容都会被填充 (显然除了照片的 CategoryId我已从 select 语句中排除的对象)。
但我希望能够在查询中包含该字段,并拥有它,以及 SQL
中查询的所有其他字段, 我可以从我的 Photo
类中排除
CategoryId
属性,并在需要 ID 时始终使用 Photo.PhotoCategory.CategoryId
。
但在某些情况下我可能会 当我获得以下对象的实例时,不想填充 PhotoCategory
对象 照片对象。
有谁知道为什么会发生上述行为?这对 Dapper 来说正常吗?
I've only just started looking at Dapper.net and have just been experimenting with some different queries, one of which is producing weird results that i wouldn't expect.
I have 2 tables - Photos
& PhotoCategories
, of which are related on CategoryID
Photos Table
PhotoId (PK - int)
CategoryId (FK - smallint)
UserId (int)
PhotoCategories Table
CategoryId (PK - smallint)
CategoryName (nvarchar(50))
My 2 classes:
public class Photo
{
public int PhotoId { get; set; }
public short CategoryId { get; set; }
public int UserId { get; set; }
public PhotoCategory PhotoCategory { get; set; }
}
public class PhotoCategory
{
public short CategoryId { get; set; }
public string CategoryName { get; set; }
{
I want to use multi-mapping to return an instance of Photo
, with a populated instance of the related PhotoCategory
.
var sql = @"select p.*, c.* from Photos p inner
join PhotoCategories c
on p.CategoryID = c.CategoryID where p.PhotoID = @pid";
cn.Open();
var myPhoto = cn.Query<Photo, PhotoCategory, Photo>(sql,
(photo, photoCategory) => { photo.PhotoCategory = photoCategory;
return photo; },
new { pid = photoID }, null, true, splitOn: "CategoryID").Single();
When this is executed, not all of the properties are getting populated (despite the same names between the DB table and in my objects.
I noticed that if I don't
'select p.* etc.' in my SQL
, and instead.
I explicitly state the fields.
I want to return EXCLUDING
p.CategoryId
from the query, then everything gets populated (except obviously the CategoryId against the Photo object which I've excluded from the select statement).
But i would expect to be able to include that field in the query, and have it, as well as all the other fields queried within the SQL
, to get populated.
I could just exclude the CategoryId
property from my Photo
class, and always use Photo.PhotoCategory.CategoryId
when i need the ID.
But in some cases I might not want to populate the PhotoCategory
object when I get an instance of
the Photo object.
Does anyone know why the above behavior is happening? Is this normal for Dapper?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚为此提交了一个修复程序:
如果
first
类型中有一个字段,而该字段恰好也在second
类型中,则多映射器会感到困惑。 .AND ... 被用作分割点。为了克服现在的短小精悍,允许
Id
字段显示在第一种类型中的任何位置。举例说明。假设我们有:
旧的拆分方法没有考虑它所映射的实际底层类型。所以...它映射了
Id =>; A
和FooId, FooId, Name => B
新方法知道
A
中的props和字段。当它第一次在流中遇到FooId
时,它不会启动分割,因为它知道A
有一个名为FooId
的属性,需要将其映射后,下次它看到FooId
时就会分裂,从而产生预期的结果。I just committed a fix for this:
The multi-mapper was getting confused if there was a field in the
first
type, that also happened to be in thesecond
type ... AND ... was used as a split point.To overcome now dapper allow for the
Id
field to show up anywhere in the first type. To illustrate.Say we have:
The old method of splitting was taking no account of the actual underlying type it was mapping. So ... it mapped
Id => A
andFooId, FooId, Name => B
The new method is aware of the props and fields in
A
. When it first encountersFooId
in the stream it does not start a split, since it knows thatA
has a property calledFooId
which needs to be mapped, next time it seesFooId
it will split, resulting in the expected results.我也有类似的问题。这是因为子级和父级对于要分割的字段具有相同的名称。例如,以下示例有效:
但以下示例无效,因为两个表和两个对象中都使用了“UserId”。
在这种情况下,Dapper 的映射似乎变得非常混乱。认为这描述了问题,但是我们可以采用解决方案/解决方法吗(抛开 OO 设计决策)?
I'm having a similar problem. It's to do with the fact that both the child and the parent have the same name for the field that is being split on. The following for example works:
But the following does not because "UserId" is used in both tables and both objects.
Dapper's mapping seems to get very confused in this scenario. Think this describes the issue but is there a solution / workaround we can employ (OO design decisions aside)?
我知道这个问题已经很老了,但我想我可以节省某人 2 分钟的时间,并给出明显的答案:只需从一个表中别名一个 id:
即:
I know this question is old but thought I would save someone 2 minutes with the obvious answer to this: Just alias one id from one table:
ie: