本文介绍了如何使用Dapper ORM获取子对象的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过以下方式检索个人资料详情:

I am retrieving profile details with the following:

var profiles = connection.Query<Models.PROFILE>("SELECT * FROM PROFILES WHERE ID=@ID", new { ID = profileID }); // IEnumerable
var profile = profiles.First<Models.PROFILE>();

配置文件对象包含其他集合,如profileImages。问题是每个子对象的项目计数为零。另外,我只想得到数据说,profileImages。

The profile object contains other collections like profileImages. The problem is that the item count for every child object is zero. Also I only want to get data for say, profileImages.

是否需要设置查询子对象的某些内容,如果是,可以指定哪一个和多少级别?

Is there something that needs to be set to query the child objects, and if so, is it possible to specify which one and for how many levels?

我也尝试过多重映射:

                var profiles = connection.Query<Models.PHOTOS_PERMISSIONS, Models.PROFILE, Models.PHOTOS_PERMISSIONS>(sql,
                    (p1, p2) => { p1.ID = profileID; return p1; }, 
                    new { ID = profileID }, 
                    splitOn: "OWNER_PROFILESIDFK, ID").AsQueryable();

PHOTOS_PERMISSIONS.OWNER_PROFILESIDFK = PROFILE.ID

PHOTOS_PERMISSIONS.OWNER_PROFILESIDFK = PROFILE.ID

得到以下错误:

我已经尝试了splitOn文本中的变化,但仍然得到相同的错误。 p>

I have tried variations of what's in my splitOn text, but still get the same error.

推荐答案

Dapper不支持像这样开箱即用的一对多映射。看看这个问题,它可能会有帮助。

Dapper doesn't support a One-To-Many mapping like this out of the box. Check out this question, it may help though.

如果您的PROFILEIMAGES表在PROFILES ID上有FK,您可以发出2个查询并使用GridReader。

If your PROFILEIMAGES table has a FK on PROFILES ID - you could issue 2 querys and use the GridReader.

var sql = 
@"
select * from PROFILES where profileId= @id
select * from PROFILEIMAGES where OWNER_PROFILESIDFK = @id";

using (var multi = connection.QueryMultiple(sql, new {id=selectedId}))
{
   var profile = multi.Read<Models.PROFILE>().Single();
   profile.ProfileImages = multi.Read<Model.PROFILEIMAGES>().ToList();
} 

这篇关于如何使用Dapper ORM获取子对象的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:22