使用dapper.net内部联接的spliton问题

时间:2015-04-16 09:47:12

标签: sql dapper

我有以下通过dapper.net执行的sql查询

  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();

1 个答案:

答案 0 :(得分:0)

SplitOn不正确,因为UserId是查询中的第一列。您必须提供从第二个表中分隔第一个表/类的列,因此第一个列/别名不属于表User_Profile而是User_Address

这似乎是AddressId

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