C# EntityCommandExecutionException未处理
我正在使用EF4.1 MVC3编写一个应用程序。我正在尝试在它上面实现CRUD。问题是,当我试图实现第一个索引页时,它给了我一个错误。我正在使用来自中央服务器的外部数据库。我只是在试一张简单的登记表。 我有第一个模型类RegModel.csC# EntityCommandExecutionException未处理,c#,asp.net-mvc-3,entity-framework,C#,Asp.net Mvc 3,Entity Framework,我正在使用EF4.1 MVC3编写一个应用程序。我正在尝试在它上面实现CRUD。问题是,当我试图实现第一个索引页时,它给了我一个错误。我正在使用来自中央服务器的外部数据库。我只是在试一张简单的登记表。 我有第一个模型类RegModel.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Registration.Models { public
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(我只是想先实现索引)。当我执行我的应用程序时,返回视图(db.Registrant.ToString())上出现错误代码>那个
“entitycommandexecutionexception未处理”
“执行命令定义时出错。有关详细信息,请参阅内部异常。”
然后,通过使用索引,我为索引创建了视图(Index.cshtml):
@model IEnumerable
@{
布局=空;
}
指数
@ActionLink(“新建”、“创建”)
用户名
密码
电子邮件
地址
@foreach(模型中的var项目){
@ActionLink(“编辑”,“编辑”,新的{id=item.id})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.id})|
@ActionLink(“删除”,“删除”,新的{id=item.id})
@item.UserName
@项目。密码
@邮件
@项目.地址
}
我正在使用这个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>
此代码存在问题:
string CmdStr = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
using (var db = new RegModelContext(CmdStr))
{
return View(db.Registrant.ToString());
}
首先,不需要显式指定连接字符串。按照惯例,实体框架将在web.config文件中查找匹配的连接字符串。只需确保连接字符串的名称与DbContext的名称匹配
其次,您正在通过db.Register
访问数据库中的Register
对象集合。对其调用ToString()
没有任何意义
您的视图需要的是注册.Models.Register的列表,而不是字符串。将代码更改为:
using (var db = new RegModelContext())
{
return View(db.Registrant.ToList());
}
请注意,这将返回数据库中的所有Register
项。这可能是一大堆数据。也许您想要实现分页或其他类型的过滤,以确保一切都按预期进行。我认为您已经创建了一个类型。前两个代码示例是相同的。你能告诉我们内部异常的细节吗?
string CmdStr = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
using (var db = new RegModelContext(CmdStr))
{
return View(db.Registrant.ToString());
}
using (var db = new RegModelContext())
{
return View(db.Registrant.ToList());
}