I'm working on a ASP.NET project using Identity (2.2). One of the things I was trying to put together was datagrid where the Users and Roles would be loaded onto one datagrid where CRUD commands could be run. I had no problem storing the select statement:
CREATE PROCEDURE [dbo].SelectUsersRoles
AS
SET NOCOUNT ON;
SELECT
p.Id, p.Email, p.PhoneNumber, p.LockoutEnabled, p.UserName,
p.AspNetUserRole, AspNetRoles.Name
FROM
AspNetUsers AS p
INNER JOIN
AspNetUserRoles ON p.Id = AspNetUserRoles.UserId
INNER JOIN
AspNetRoles ON AspNetUserRoles.RoleId = AspNetRoles.Id
GO
And this selects the data and I can load the grid just fine. After reviewing this question, and this one, I was left with the idea that the correct update code should have been:
[EDIT, ran as stored procedure updated below, same issue]
CREATE PROCEDURE [dbo].UpdateUserRoles
(
@Email nvarchar(256),
@UserName nvarchar(256),
@PhoneNumber nvarchar(MAX),
@LockoutEnabled bit,
@original_ID nvarchar(128)
)
AS
SET NOCOUNT ON;
UPDATE p
SET p.Email = @Email,
p.Username = @UserName,
p.PhoneNumber = @PhoneNumber,
p.LockoutEnabled = @LockoutEnabled
FROM [AspNetUsers] AS p
INNER JOIN [AspNetUserRoles] AS r ON p.ID = r.UserID
INNER JOIN [AspNetRoles] AS b ON r.RoleID = b.ID
WHERE p.ID = @original_ID
GO
If I try to use the Query Builder, I notice VS'13 automatically adds a CROSS JOIN
statement to it: [and generally makes it less readable]
UPDATE p
SET p.Email = @Email, p.UserName = @UserName,
p.PhoneNumber = @PhoneNumber, p.LockoutEnabled = @LockoutEnabled
FROM
AspNetUsers AS p
INNER JOIN
AspNetUserRoles AS r ON p.Id = r.UserId
INNER JOIN
AspNetRoles AS b ON r.RoleId = b.Id
CROSS JOIN
p
WHERE
(p.Id = @original_ID)
And this keeps throwing the error that 'p' is an invalid object when I try to run the query.
Is there something simple I'm missing, or would this be better to split into consecutive update commands? The end goal is to update both the user information (ie phone number) along with the role assigned to the user.
Just to have the DB set up in question, I think you could start any new MVC/Web Forms template from Visual Studio, make a few users and put in some roles.