简体   繁体   English

使用Dapper一对多映射

[英]Mapping one to many with Dapper

Trying to figure this out, but I can't get it to work. 试图弄清楚这一点,但我无法正常工作。 This query: 该查询:

select MultiCollections.*, Collections.* from MultiCollections 
left join MultiCollectionCollections on MultiCollections.Id = MultiCollectionCollections.MultiCollectionId
left join  Collections on MultiCollectionCollections.CollectionId = Collections.Id
where MultiCollections.UserId=5

This will return this data: 这将返回以下数据:

在此处输入图片说明 As you can see, row 1 and 2 are from the same Title. 如您所见,第1行和第2行来自同一标题。 The data behind them are books. 它们背后的数据是书籍。 Row 3 and 4 are also collections but don't have books. 第3行和第4行也是馆藏,但没有书籍。

I have two objects in my code: MultiCollection Collection 我的代码中有两个对象:MultiCollection集合

Both correspond with the data given in the result of the query: Id, UserId and Title are for object MultiCollection. 两者都与查询结果中给出的数据相对应:Id,UserId和Title用于对象MultiCollection。 Other data are for the object Collection. 其他数据用于对象Collection。

I expect to see three MultiCollections in my C# code: Action Drama Fiction 我希望在我的C#代码中看到三个MultiCollection:Action Drama Fiction

Action will have 2 collections. 动作将有2个集合。 Drama and Fiction should be empty. 戏剧和小说应该是空的。

Instead, I get 4 MultiCollections and none of them contains Collections. 相反,我得到了4个MultiCollections,它们都不包含Collections。 My C# code: 我的C#代码:

public IEnumerable<MultiCollection> GetAll(int userId)
    {
        string query = @"select MC.*, C.* from MultiCollections  MC
                        left join MultiCollectionCollections MCC on MC.Id = MCC.MultiCollectionId
                        left join  Collections C on MCC.CollectionId = C.Id
                        where UserId=" + userId;

        using (DbConnection connection = ConnectionFactory())
        {
            connection.Open();
            return connection.Query<MultiCollection, List<Collection>, MultiCollection>(query,
                (a, s) =>
                {
                    a.Collections = s;
                    return a;
                });
        }
    }

When running the code I would expect this: 运行代码时,我期望这样:

Action    
    Collections    
       -> Book 1    
       -> Book 2     
Drama    
   Collections    
       Null     
Fiction    
    Collections    
        Null

I have no idea what I'm doing wrong. 我不知道我在做什么错。

Your c# code should of look like this: 您的C#代码应如下所示:

public IEnumerable<MultiCollection> GetAll(int userId)
{
    string query = @"select MC.*, C.* from MultiCollections  MC
                    left join MultiCollectionCollections MCC on MC.Id = MCC.MultiCollectionId
                    left join  Collections C on MCC.CollectionId = C.Id
                    where UserId=" + userId;

    using (DbConnection connection = ConnectionFactory())
    {
        connection.Open();
        return connection.Query<MultiCollection, Collection, MultiCollection>(query,
            (a, s) =>
            {
                a.Collections = new List<Collection>();
                a.Collections.Add(s);
                return a;
            }, splitOn: "MultiCollectionId,CollectionId"););
    }
}

Notice, that .Query<MultiCollection, Collection, MultiCollection> is a Collection not List<Collection> and it is doing .add() and not setter. 通知,即.Query<MultiCollection, Collection, MultiCollection>是一个CollectionList<Collection>和它做.add()而不是设置器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM