2014-11-03 28 views
0

目前玩弄小巧玲瓏我試圖插入值到數據庫如下錯誤使用SqlMapperExtension小巧玲瓏插入時

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. 

我用Google搜索這個錯誤,但我誤解了所提供的答案。

+0

任何人都可以幫忙嗎? – 2014-11-05 05:27:55

回答

0

從錯誤消息中我假設你已經開始了一個既沒有提交也沒有回滾的事務。這個錯誤信息的真正原因是在其他地方。

我建議你在Context.ReturnDatabaseConnection()中記錄請求並追蹤出現這個錯誤的請求。

另外我建議你在代碼中查看所有事務並檢查它們是否正確完成(提交/回滾)。