简体   繁体   English

使用Dapper映射到嵌套对象不起作用

[英]Using Dapper to map to nested objects not working

The Problem 问题

Simply put, the mapping does not perform as expected. 简而言之,映射无法按预期执行。 The local variable reason comes back as null so the assignment to nav sets the nested object Reason to null as well. 局部变量reason返回为null,因此对nav的赋值也会将嵌套对象Reason为null。

Am I missing something simple here? 我在这里缺少简单的东西吗?

The Stored Procedure 存储过程

ALTER PROCEDURE [PA].[spGetDocumentNavById]
-- Add the parameters for the stored procedure here
@DocumentNavID INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
SELECT
    dn.DocumentNavID,
    dn.IssuerName,
    nt.Name,
    dn.Amount,
    dn.BTF,
    dn.NavDate,
    dn.ReceivedDate,
    dn.PrimaryIdentifier,
    dn.AssetID,
    dh.LastModifiedBy,
    dh.IsApproved,
    dh.Corrected,
    dh.ReasonID
FROM PA.DocumentNavs dn WITH(NOLOCK)
    JOIN PA.NAVTypes nt WITH(NOLOCK)
        ON nt.NAVTypeID = dn.NavTypeID
    LEFT JOIN PA.DocumentsHistory dh WITH(NOLOCK)
        ON dh.DocumentID = dn.DocumentID
    LEFT JOIN PA.Analysts a WITH(NOLOCK)
        ON a.AnalystID = dh.LastModifiedBy
WHERE dn.DocumentNavID = @DocumentNavID

The C# C#

public DocumentNav FindById(int documentNavId)
    {
        var data = new
        {
            documentNavId
        };

        using (var conn = SqlConnection)
        {
            var response = conn.Query<DocumentNav, Reason, DocumentNav>("[PA].spGetDocumentNavById",
                map: (nav, reason) =>
                {
                    nav.Reason = reason;
                    return nav;
                }, param: data, splitOn: "ReasonID", commandType: System.Data.CommandType.StoredProcedure)
                .DefaultIfEmpty().FirstOrDefault();

            return response;
        }
    }

Dapper is able to split the returned row query by Id columns ("Id" or "id"). Dapper可以按ID列(“ Id”或“ id”)拆分返回的行查询。 If your primary key is different or you would like to split the row at a point other than Id, use the optional "splitOn" parameter. 如果您的主键不同,或者您想在ID以外的其他位置拆分行,请使用可选的“ splitOn”参数。
In your example the query should look like: 在您的示例中,查询应类似于:

SELECT
    -- DocumentNav Object Columns 
    dn.DocumentNavID AS "Id",
    dn.IssuerName,
    -- ... all other DocumentNav properties..
    -- Reason Object Columns
    dh.ReasonID AS "Id"
    dh.LastModifiedBy,
    dh.IsApproved,
    dh.Corrected
    -- ... all other Reason properties..
FROM PA.DocumentNavs dn WITH(NOLOCK)
    JOIN PA.NAVTypes nt WITH(NOLOCK)
        ON nt.NAVTypeID = dn.NavTypeID
    LEFT JOIN PA.DocumentsHistory dh WITH(NOLOCK)
        ON dh.DocumentID = dn.DocumentID
    LEFT JOIN PA.Analysts a WITH(NOLOCK)
        ON a.AnalystID = dh.LastModifiedBy
WHERE dn.DocumentNavID = @DocumentNavID

You need your objects to be with property name "Id" (DocumentNav & Reason) 您需要对象具有属性名称“ Id”(DocumentNav和Reason)

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

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