使用dapper.net內部聯接的spliton問題

dapper sql

我有以下sql查詢,它通過dapper.net執行

  var resultList = sqlCon.Query<UserProfile, UserAddress, UserProfile>(@"

                            UPDATE [User].[User_Profile]
                                SET ProfileStatus = 4
                            WHERE Id = @UserId

                            SELECT u.Id [UserId], u.Username, u.Age,
                                   u.ProfileStatus,
                                   a.Id [AddressId], a.Country, a.[State], a.City,
                                   a.Latitude, a.Longitude
                            FROM [User].[User_Profile] u
                            INNER JOIN [User].[User_Address] a on u.id = a.UserId
                            WHERE u.Id = @UserId", (u, a) =>
                            {
                                u.Id = a.UserId;
                                return u;
                            },
                            new { UserId = userId }, splitOn: "UserId").FirstOrDefault();

運行時我收到此錯誤:

當使用多映射API時,如果您有除Id之外的其他鍵,請確保設置splitOn參數,“splitOn”

我的兩個課程如下:

首先是我試圖填充的userProfile

 public class UserProfile
{
    public Int64 UserId { get; set; }

    public string UserName { get; set; }

    public int Age { get; set; }

    public int ProfileStatus { get; set; }

    public Int64 AddressId { get; set; }

    public int Country { get; set; }

    public int State { get; set; }

    public int City { get; set; }

    public decimal Latitude { get; set; }

    public decimal Longitude { get; set; }
}

這是我的用戶地址類:

 public class UserAddress
{
    public Int64 Id { get; set; }

    public Int64 UserId { get; set; }

    public int Country { get; set; }

    public int State { get; set; }

    public int Town { get; set; }

    public string PostCode { get; set; }

    public decimal Latitude { get; set; }

    public decimal Longitude { get; set; }
}

我不知道我做錯了什麼,因為我認為我已經說過在UserId上拆分了?但顯然情況似乎並非如此?

**更新**

使用別名更新sql語句

                            SELECT u.Id as [UserId], u.Username as [Username], u.Age as [Age],
                                   u.ProfileStatus as [ProfileStatus], 
                                   a.Id as [AddressId], a.Country as [Country], a.[State] as [State],
                                   a.City as [City], a.Latitude as [Latitude], a.Longitude as [Longitude]
                            FROM [User].[User_Profile] u
                            INNER JOIN [User].[User_Address] a on u.id = a.UserId
                            WHERE u.Id = @UserId", (u, a) =>
                            {
                                u.UserId = a.UserId;
                                return u;
                            },
                            new { UserId = userId }, splitOn: "AddressId").FirstOrDefault();

熱門答案

SplitOn不正確,因為UserId是查詢中的第一列。您必須提供從第二個表中User_Profile第一個表/類的列,因此第一個列/別名不屬於表User_Profile但屬於User_Address

這似乎是AddressId

// ...
new { UserId = userId }, splitOn: "AddressId").FirstOrDefault();


許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因