I have the following sql query which is executed via 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();
I receive this error when running it:
When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id“, â€splitOn
My two classes are as follows:
first is the userProfile which im trying to populate
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; }
}
and this is my User Address Class:
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; }
}
I'm not sure what I'm doing wrong as I think I have said to split on the UserId? but clearly that doesn't seem to be the case?
** Update **
Update sql statement with alias
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();
The SplitOn
is incorrect because UserId
is the first column in your query. You have to provide the column that delimits the first table/class from the second, so the first column/alias that does not belong to the table User_Profile
but User_Address
.
That seems to be AddressId
.
// ...
new { UserId = userId }, splitOn: "AddressId").FirstOrDefault();