i got a problem using dapper and MySql in a ASP.net Identity project. I want to insert a user to a table users and want the autogenerated id from this insertation back. But i get a syntax error and i don“t know why.
this is my code for the method insert:
public void Insert(TUser member)
{
var id = db.Connection.ExecuteScalar<int>(@"Insert into users
(UserName, PasswordHash, SecurityStamp,Email,EmailConfirmed,PhoneNumber,PhoneNumberConfirmed, AccessFailedCount,LockoutEnabled,LockoutEndDateUtc,TwoFactorEnabled)
values (@name, @pwdHash, @SecStamp,@email,@emailconfirmed,@phonenumber,@phonenumberconfirmed,@accesscount,@lockoutenabled,@lockoutenddate,@twofactorenabled)
SELECT Cast(LAST_INSERT_ID() as AS UNSIGNED INTEGER)",
new
{
name = member.UserName,
pwdHash = member.PasswordHash,
SecStamp = member.SecurityStamp,
email = member.Email,
emailconfirmed = member.EmailConfirmed,
phonenumber = member.PhoneNumber,
phonenumberconfirmed = member.PhoneNumberConfirmed,
accesscount = member.AccessFailedCount,
lockoutenabled = member.LockoutEnabled,
lockoutenddate = member.LockoutEndDateUtc,
twofactorenabled = member.TwoFactorEnabled
});
// we need to set the id to the returned identity generated from the db
member.Id = id;
}
and this is my table users:
CREATE TABLE `users` (
`Id` int(36) NOT NULL,
`Email` varchar(256) DEFAULT NULL,
`EmailConfirmed` tinyint(1) NOT NULL,
`PasswordHash` longtext,
`SecurityStamp` longtext,
`PhoneNumber` longtext,
`PhoneNumberConfirmed` tinyint(1) NOT NULL,
`TwoFactorEnabled` tinyint(1) NOT NULL,
`LockoutEndDateUtc` datetime DEFAULT NULL,
`LockoutEnabled` tinyint(1) NOT NULL,
`AccessFailedCount` int(11) NOT NULL,
`UserName` varchar(256) NOT NULL,
`FirstName` varchar(50) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
ALTER TABLE `users`
ADD PRIMARY KEY (`Id`);
ALTER TABLE `users`
MODIFY `Id` int(36) NOT NULL AUTO_INCREMENT;
Error msg: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT Cast(LAST_INSERT_ID() as AS UNSIGNED INTEGER)' at line 4
I thought LAST_INSERT_ID is giving me the last auto incerement userid?
Thanks for help