小巧玲珑的多选:嵌套类主键没有映射,除非使用" AS" [英] Dapper multiselect: Nested class primary key doesn't map unless using "AS"

查看:169
本文介绍了小巧玲珑的多选:嵌套类主键没有映射,除非使用" AS"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类:

class Foo{
    public int FooId { get; set; }
    ...
    public Bar Bar { get; set }
}

class Bar{
    public int BarId { get; set; }
    public int FooId { get; set }
    ...
}

当我运行查询是这样的:

when i then run the query like this:

sqlConnection.Query<Foo, Bar, Foo>(
    "SELECT * FROM Foo JOIN Bar ON Foo.FooId = Bar.FooId",
    (foo, bar) => { 
         foo.Bar = bar;
         return foo; 
       }, 
    splitOn: "FooId");

结果将被这两个Foo和酒吧的所有属性将映射了除Bar.BarId。 在数据库中检查列名和类型对我的酒吧下课后我还是没找到任何差异。

the result would then be that all properties on both Foo and Bar will map up Except for Bar.BarId. After checking the column name and type in the database against my Bar class I still couldn't find any differences.

一个奇怪的事情,我偶然发现是,如果我写的:

One strange thing I stumbled upon was that if I wrote:

"SELECT *, BarId AS BarId FROM Foo JOIN Bar ON Foo.FooId = Bar.FooId"

Bar.BarId实际上映射为预期的,有我误解了如何使用小巧精致,或这是一个错误?

Bar.BarId actually mapped as expected, have I misunderstood how to use Dapper or is this a bug?

推荐答案

这是试图做一个分割 FooId ,所以每次它看到 FooId 这是切割的数据。这个用例是的实际上的预期为(并不少见)场景中的所有表有predictable键,如编号。在你的情况,这是不是你想要的,当你从数据库中获取:

It is trying to do a split on FooId, so every time it sees FooId it is cutting the data. This use-case is essentially intended for the (not uncommon) scenario where all the tables have a predictable key such as Id. In your case, this is not what you want, as you get from the database:

FooId, a, b, c | BarId, FooId, x, y, z
^^ from Foo ^^ | ^^ from Bar ^^

然而,在 FooId 一个分割为:

FooId, a, b, c, BarId | FooId, x, y, z

这是为什么 BarId 不获取包括在第二对象,并且还为什么将其加入到最终使得它的工作。

which is why BarId doesn't get included in the second object, and also why adding it to the end makes it work.

还有另一种用法,IIRC,它接受的测序的键分裂;你可以使用:

There is another usage, IIRC, that accepts the sequenced keys to split on; you would use:

splitOn: "FooId,BarId"

这篇关于小巧玲珑的多选:嵌套类主键没有映射,除非使用&QUOT; AS&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆