Ошибка при вставке с использованием SqlMapperExtension Dapper
В настоящее время играет с Dapper Я пытаюсь вставить значения в БД следующим образом
using (var sqlCon = new SqlConnection(Context.ReturnDatabaseConnection()))
{
sqlCon.Open();
try
{
var emailExists = sqlCon.Query<UserProfile>(@"SELECT UserId FROM User_Profile WHERE EmailAddress = @EmailAddress",
new { EmailAddress = userRegister.EmailAddress.Trim() }).FirstOrDefault();
if (emailExists == null) // No profile exists with the email passed in, so insert the new user.
{
userProfile.UniqueId = Guid.NewGuid();
userProfile.Firstname = userRegister.Firstname;
userProfile.Surname = userRegister.Surname;
userProfile.EmailAddress = userRegister.EmailAddress;
userProfile.Username = CreateUsername(userRegister.Firstname);
userProfile.Password = EncryptPassword(userRegister.Password);
userProfile.AcceptedTerms = true;
userProfile.AcceptedTermsDate = System.DateTime.Now;
userProfile.AccountActive = true;
userProfile.CurrentlyOnline = true;
userProfile.ClosedAccountDate = null;
userProfile.JoinedDate = System.DateTime.Now;
userProfile.UserId = SqlMapperExtensions.Insert(sqlCon, userProfile); // Error on this line
Registration.SendWelcomeEmail(userRegister.EmailAddress, userRegister.Firstname); // Send welcome email to new user.
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
sqlCon.Close();
}
}
Я получаю ошибку
ExecuteNonQuery requires the command to have a transaction when the connection
assigned to the command is in a pending local transaction. The Transaction
property of the command has not been initialized.
Я погуглил эту ошибку, но я неправильно понял предоставленные ответы.
1 ответ
Из сообщения об ошибке я предполагаю, что вы запустили транзакцию, которая не была ни зафиксирована, ни откатана. Настоящая причина этого сообщения об ошибке в другом месте.
Я предлагаю вам войти запросы Context.ReturnDatabaseConnection()
и проследите, какие запросы предшествуют этой ошибке.
Также я советую вам посмотреть в своем коде все транзакции и проверить, правильно ли они выполнены (commit/rollback).