EntityCommandExecutionException了未处理 [英] EntityCommandExecutionException was unhandled

查看:117
本文介绍了EntityCommandExecutionException了未处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写使用EF 4.1 MVC3的应用程序。我想实现它的CRUD。问题是,当我尝试实施只是第一索引页它给了我一个错误。我使用的外部数据库从中央服务器。我只是想一个简单的登记表。
我第一次模型类RegModel.cs

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;命名空间Registration.Models
{
    公共类注册
    {
        公众诠释标识{搞定;组; }
        公共字符串用户名{获得;组; }
        公共字符串密码{搞定;组; }
        公共字符串电子邮件{获得;组; }
        公共字符串地址{搞定;组; }
    }
}

然后,RegModelContext.cs

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;命名空间Registration.Models
{
    公共类注册
    {
        公众诠释标识{搞定;组; }
        公共字符串用户名{获得;组; }
        公共字符串密码{搞定;组; }
        公共字符串电子邮件{获得;组; }
        公共字符串地址{搞定;组; }
    }
}

我的控制器的名字是RegController.cs(我只是想率先实行指数)的控制器。当我执行我的应用程序它给了我错误的返回查看(db.Registrant.ToString());
 entitycommandexecutionexception了未处理
 在执行命令定义时出错。详情请参阅内部异常。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;
使用Registration.Models;
使用System.Configuration;
命名空间Registration.Controllers
{
    公共类RegController:控制器
    {
        //
        // GET:/注册/
        私人字符串CmdStr = ConfigurationManager.ConnectionStrings [的myconn]的ConnectionString。
        公众的ActionResult指数()
        {
            使用(VAR DB =新RegModelContext(CmdStr))
            {
                返回查看(db.Registrant.ToString());
            }        }    }
}

然后,通过使用索引创建视图索引(Index.cshtml):

  @model IEnumerable的< Registration.Models.Register>@ {
    布局= NULL;
}<!DOCTYPE HTML>< HTML和GT;
< HEAD>
    <标题>指数< /标题>
< /头>
<身体GT;
    &所述p为H.;
        @ Html.ActionLink(新建,创建)
    &所述; / P>
    <表>
        &所述; TR>
            百分位>< /第i
            <第i个
                用户名
            < /第i
            <第i个
                密码
            < /第i
            <第i个
                电子邮件
            < /第i
            <第i个
                地址
            < /第i
        < / TR>    @foreach(以型号VAR项){
        &所述; TR>
            &所述; TD>
                @ Html.ActionLink(编辑,编辑,新{ID = item.Id})|
                @ Html.ActionLink(详细信息,详细信息,新{ID = item.Id})|
                @ Html.ActionLink(删除,删除,新{ID = item.Id})
            < / TD>
            &所述; TD>
                @ item.UserName
            < / TD>
            &所述; TD>
                @ item.Password
            < / TD>
            &所述; TD>
                @ item.Email
            < / TD>
            &所述; TD>
                @ item.Address
            < / TD>
        < / TR>
    }    < /表>
< /身体GT;
< / HTML>

我用这web.config文件

 <?XML版本=1.0编码=UTF-8&GT?;
<! -
  有关如何配置ASP.NET应用程序的更多信息,请访问:
  http://go.microsoft.com/fwlink/?LinkId=152368
   - >
<结构>
  < configSections>
    <! - 有关Entity Framework的配置的详细信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468 - >
    <节名称=的EntityFrameworkTYPE =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,的EntityFramework,版本4.3.1.0 =文化=中性公钥= b77a5c561934e089/>
  < / configSections>
  <&的appSettings GT;
    <添加键=ClientValidationEnabledVALUE =真/>
    <添加键=UnobtrusiveJavaScriptEnabledVALUE =真/>
  < /的appSettings>
  <&的System.Web GT;
    <编译调试=真targetFramework =4.0>
      <&集会GT;
        <添加组件=System.Web.Abstractions,版本= 4.0.0.0,文化=中性公钥= 31BF3856AD364E35/>
        <添加组件=System.Web.Helpers,版本= 1.0.0.0,文化=中性公钥= 31BF3856AD364E35/>
        <添加组件=System.Web.Routing,版本= 4.0.0.0,文化=中性公钥= 31BF3856AD364E35/>
        <添加组件=System.Web.Mvc,版本= 3.0.0.0,文化=中性公钥= 31BF3856AD364E35/>
        <添加组件=System.Web.WebPages,版本= 1.0.0.0,文化=中性公钥= 31BF3856AD364E35/>
      < /组件>
    < /编译>
    <身份验证模式=表格>
      <形式loginUrl =〜/帐号/登录超时=2880/>
    < /认证>
    <网页和GT;
      <&命名空间GT;
        <添加命名空间=System.Web.Helpers/>
        <添加命名空间=System.Web.Mvc/>
        <添加命名空间=System.Web.Mvc.Ajax/>
        <添加命名空间=System.Web.Mvc.Html/>
        <添加命名空间=System.Web.Routing/>
        <添加命名空间=System.Web.WebPages/>
      < /命名空间>
    < /页>
  < /system.web>
  < system.webServer>
    <验证validateIntegratedModeConfiguration =FALSE/>
    <模块runAllManagedModulesForAllRequests =真/>
  < /system.webServer>
  <&运行GT;
    < assemblyBinding的xmlns =瓮:架构 - 微软COM:asm.v1>
      < dependentAssembly>
        < assemblyIdentity名称=System.Web.Mvc公钥=31bf3856ad364e35/>
        < bindingRedirect oldVersion =1.0.0.0-2.0.0.0NEWVERSION =3.0.0.0/>
      < / dependentAssembly>
    < / assemblyBinding>
  < /运行>
  <&是connectionStrings GT;
    <添加名称=的myconn的connectionString =数据源= WIN-A32JMN2HC3A \\ SAIG​​MAMSSQL;初始目录=注册;用户ID = SAIG​​MA110;密码= 12345;/>
  < /&是connectionStrings GT;
< /结构>


解决方案

现在的问题是这个code:

 字符串CmdStr = ConfigurationManager.ConnectionStrings [的myconn]的ConnectionString。
使用(VAR DB =新RegModelContext(CmdStr))
{
    返回查看(db.Registrant.ToString());
}

首先,你并不需要明确指定的连接字符串。按照惯例,实体框架将查找在web.config文件中匹配的连接字符串。只要确保连接字符串的名称,你的DbContext的名称相匹配。

第二,你正在访问注册对象的集合在你的数据库低谷 db.Registrant 。调用的ToString()上它没有任何意义。

您认为预计 Registration.Models.Register 的列表,而不是一个字符串。更改code这样:

 使用(VAR DB =新RegModelContext())
{
    返回查看(db.Registrant.ToList());
}

请注意,此函数返回数据库中的所有注册项目。这可能是一大堆数据。也许你想实现分页或其他类型的过滤,以确保一切正常。

I am writing an application using EF 4.1 MVC3. I am trying to implement CRUD on it. The problem is that when i try to implement just first index page it gave me an error. I am using external database from central server. I am just trying a simple Registration form. I have first model class RegModel.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; }
    }
}

Then a 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; }
    }
}

My controller name is RegController.cs (I am just trying to implement Index first.) in controller. When i am executing my application it gave me error on return View(db.Registrant.ToString()); that "entitycommandexecutionexception was unhandled" "An error occurred while executing the command definition. See the inner exception for details."

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());
            }

        }

    }
}

Then by using Index I create view for index (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>

I am using this web.config file

<?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>

解决方案

The problem is with this code:

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

First, you don't need to specify the connection string explicitly. By convention the Entity Framework will look for a matching connection string in your web.config file. Just make sure the name of the connection string matches the name of your DbContext.

Second, you are accessing a collection of Register objects in your database trough db.Registrant. Calling ToString() on it doesn't make any sense.

Your view expects a list of Registration.Models.Register instead of a string. Change your code to this:

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

Be aware that this is returning all Register items in your database. That could be a whole lot data. Maybe you want to implement paging or another type of filtering to make sure that everything works as expected.

这篇关于EntityCommandExecutionException了未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆