2012-08-08 89 views
1

我正在使用EF 4.1 MVC3編寫應用程序。我正試圖在其上實施CRUD。問題是,當我嘗試只實現第一個索引頁時,它給了我一個錯誤。我正在使用中央服務器的外部數據庫。我只是嘗試一個簡單的註冊表單。 我第一個模型類RegModel.csEntityCommandExecutionException未處理

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace Registration.Models 
{ 
    public class Register 
    { 
     public int Id { get; set; } 
     public string UserName { get; set; } 
     public string Password { get; set; } 
     public string Email { get; set; } 
     public string Address { get; set; } 
    } 
} 

然後,RegModelContext.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace Registration.Models 
{ 
    public class Register 
    { 
     public int Id { get; set; } 
     public string UserName { get; set; } 
     public string Password { get; set; } 
     public string Email { get; set; } 
     public string Address { get; set; } 
    } 
} 

我的控制器的名字是RegController.cs(我只是想率先實行指數)的控制器。當我執行我的應用程序時,它給了我return View(db.Registrant.ToString());上的錯誤 「entitycommandexecutionexception was unledled」 「執行命令定義時發生錯誤,請參閱內部異常的詳細信息。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using Registration.Models; 
using System.Configuration; 


namespace Registration.Controllers 
{ 
    public class RegController : Controller 
    { 
     // 
     // GET: /Reg/ 
     private string CmdStr = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;  
     public ActionResult Index() 
     { 
      using (var db = new RegModelContext(CmdStr)) 
      { 
       return View(db.Registrant.ToString()); 
      } 

     } 

    } 
} 

然後使用索引創建視圖索引(Index.cshtml):

@model IEnumerable<Registration.Models.Register> 

@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <title>Index</title> 
</head> 
<body> 
    <p> 
     @Html.ActionLink("Create New", "Create") 
    </p> 
    <table> 
     <tr> 
      <th></th> 
      <th> 
       UserName 
      </th> 
      <th> 
       Password 
      </th> 
      <th> 
       Email 
      </th> 
      <th> 
       Address 
      </th> 
     </tr> 

    @foreach (var item in Model) { 
     <tr> 
      <td> 
       @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | 
       @Html.ActionLink("Details", "Details", new { id=item.Id }) | 
       @Html.ActionLink("Delete", "Delete", new { id=item.Id }) 
      </td> 
      <td> 
       @item.UserName 
      </td> 
      <td> 
       @item.Password 
      </td> 
      <td> 
       @item.Email 
      </td> 
      <td> 
       @item.Address 
      </td> 
     </tr> 
    } 

    </table> 
</body> 
</html> 

我用這web.config文件

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=152368 
    --> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
    </configSections> 
    <appSettings> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0"> 
     <assemblies> 
     <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
     <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
     <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
     <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
     <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
     </assemblies> 
    </compilation> 
    <authentication mode="Forms"> 
     <forms loginUrl="~/Account/LogOn" timeout="2880" /> 
    </authentication> 
    <pages> 
     <namespaces> 
     <add namespace="System.Web.Helpers" /> 
     <add namespace="System.Web.Mvc" /> 
     <add namespace="System.Web.Mvc.Ajax" /> 
     <add namespace="System.Web.Mvc.Html" /> 
     <add namespace="System.Web.Routing" /> 
     <add namespace="System.Web.WebPages" /> 
     </namespaces> 
    </pages> 
    </system.web> 
    <system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <modules runAllManagedModulesForAllRequests="true" /> 
    </system.webServer> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <connectionStrings> 
    <add name="MyConn" connectionString="Data Source=WIN-A32JMN2HC3A\SAIGMAMSSQL;Initial Catalog=Registration;User Id=SAIGMA110;Password=12345;"/>  
    </connectionStrings> 
</configuration> 
+0

我想你是一個類型。你的前兩個代碼示例是相同的。你能告訴我們內部異常的細節嗎? – 2012-08-08 11:01:26

回答

3

問題是這段代碼:

string CmdStr = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString; 
using (var db = new RegModelContext(CmdStr)) 
{ 
    return View(db.Registrant.ToString()); 
} 

首先,你沒有n eed明確指定連接字符串。按照慣例,實體框架將在您的web.config文件中查找匹配的連接字符串。只要確保連接字符串的名稱與您的DbContext的名稱相匹配即可。

其次,您正在訪問數據庫中的Register對象的集合槽db.Registrant。調用ToString()就沒有任何意義。

您的看法期望Registration.Models.Register列表而不是字符串。你的代碼改成這樣:

using (var db = new RegModelContext()) 
{ 
    return View(db.Registrant.ToList()); 
} 

要知道,這是返回數據庫中的所有Register項目。這可能是一個很大的數據。也許你想實現分頁或其他類型的過濾,以確保一切按預期工作。