使用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
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow