2013-05-01 156 views
1

我正在研究一個新的ASP.Net MVC 4應用程序並測試用戶登錄。我收到以下異常:System.Data.SqlClient.SqlException:無效的列名'Email'

System.Data.SqlClient.SqlException: Invalid column name 'Email' 

我使用http://kevin-junghans.blogspot.com/2013/02/adding-email-confirmation-to.html的示例。

下面是相應的代碼:

[AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public ActionResult Register(RegisterModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       // Attempt to register the user 
       try 
       { 
        string confirmationToken = 
         WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Email = model.Email }, true); 

        System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("[email protected]"); 
        System.Net.Mail.MailAddress[] to = new System.Net.Mail.MailAddress[1]; 
        to[1] = new System.Net.Mail.MailAddress(model.Email); 
        System.Net.Mail.MailAddress[] cc = new System.Net.Mail.MailAddress[0]; 
        System.Net.Mail.MailAddress[] bcc = new System.Net.Mail.MailAddress[0]; 
        String Subject = "Please verify your e-mail address."; 
        String body = String.Format("Thank you for registering with CardMage." + 
         "To verify your account, please follow this link." + 
         "http://www.cardmage.co/Account/RegisterConfirmation/{0}", confirmationToken); 
        System.Net.NetworkCredential nc = new NetworkCredential("foo", "bar"); 
        SendGrid sg = SendGrid.GetInstance(from, to, cc, bcc, Subject, body, ""); 
        SMTP s = SMTP.GetInstance(nc); 
        s.Deliver(sg); 
        return RedirectToAction("RegisterStepTwo", "Account"); 

我在做什麼錯?我試圖從谷歌搜索找到解決方案,但沒有運氣。我發現this SO問題的屬性值格式與我一樣,所以我檢查了我的數據庫,但Email列不存在。有人可以請指點我正確的方向嗎?感謝您的時間和考慮。

+1

我假設你在調用CreateUserAndAccount時遇到了錯誤,並且如果你查看數據庫,Table UserProfile沒有Email列。請提供UserProfile類定義的代碼,數據庫初始值設定項和Global.asax Application_Start方法。請注意,本文假定您遵循上一篇文章中關於自定義和種植SimpleMembership的步驟。 http://kevin-junghans.blogspot.com/2013/01/seeding-customizing-aspnet-mvc.html – 2013-05-01 19:10:56

回答

3

要獲得通過調用CreateUserAndAccount更新您的電子郵件字段,請執行下列操作:

1確保您正在使用的數據庫表有一個電子郵件列。

2更改帳戶模型UserProfile以包含您的電子郵件字段。

3更改註冊視圖以包含您的電子郵件。

4更改的AccountController到包括anomymous對象(這個你這樣做)

然後,當你註冊一個新用戶,其電子郵件將被存儲在數據庫中的表。

這適用於我。

+1

這些列是自動生成的,如何保留自定義列在「CreateUserAndAccount」調用之前創建? – 2013-07-29 10:32:02