System.Data.Entity.Core.EntityCommandExecutionException发生在MVC应用程序中使用EF [英] System.Data.Entity.Core.EntityCommandExecutionException Occurred in MVC app Using EF

查看:5190
本文介绍了System.Data.Entity.Core.EntityCommandExecutionException发生在MVC应用程序中使用EF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我即将与UUID创建申请表作为其唯一键(不主键)



我得到了错误:




类型'System.Data.Entity.Core.EntityCommandExecutionException的异常出现在EntityFramework.SqlServer.dll但在用户代码
附加信息没有处理:执行命令定义时出错。 。详情请参见内部异常




连接字符串DB:

 的connectionString =数据源=(的LocalDB)\v11.0; 
初始目录= FormsContext;
集成安全=真;
MultipleActiveResultSets = TRUE;
AttachDbFilename = | DataDirectory目录| Forms.mdf
的providerName =System.Data.SqlClient的

FormsContext.cs:

 命名空间ApplicationForm.Models 
{
公共类FormsContext:的DbContext
{
公共FormsContext():基地(NAME = FormsContext)
{
}

公共System.Data.Entity.DbSet< ApplicationForm.Models.Form>表格{搞定;组; }
...



该数据库是这样的:

  FormsContext->&形式提出GT; {ID,UUID,第一个,最后...} 

我不那么肯定发生了什么事这个时候。会有人帮我办理呢?



修改/分辨率



我不知道如何检查内部异常和我做了一些这方面的研究。所以,当我在内部异常读取错误信息,我可以解决我的问题。



我发现了什么错误了。查询字段是'姓',但我的表的列名是第一。他们不匹配。有一次,我改变了DB列名,这是再次合作。


解决方案

发现了什么的bug了。查询字段是'姓',但我的表的列名是第一。他们不匹配。有一次,我改变了DB列名,这是再次合作。




一个人必须要勤快地保留每个同步如果数据库始终在不断变化。



为什么?



EF简直是映射,无论是表或存储过程和任何微妙的变化列名或外键约束可以拥有的纹波的效果。



这些影响可以创建显而易见的,就像你发现错误或无明显的逻辑错误,它可以提供工作(编译)软件与微妙的逻辑错误。的我已经看到经验逻辑错误,他们确实发生了。



所以,时时刻刻提防保持同步的EF映射。






我回答到不同的EF的问题,但与此类似。



我已经碰到一种情况,在EF在运行时映射存储过程失败,因为内部的变更的在存储过程看到一个sql 已剥离出从结果列名和EF(前更改映射)失败。我有我的回答的屏幕截图这表明EF / DB的变化可以有不利影响:



数据读取器不兼容。成员不具备与同名的数据读取相应的列


I am about to create an Application Form with UUID as its unique key (not primary key).

I got the error:

An exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll but was not handled in user code Additional information: An error occurred while executing the command definition. See the inner exception for details.

Connection String To DB:

 connectionString="Data Source=(localdb)\v11.0; 
                   Initial Catalog=FormsContext; 
                   Integrated Security=True; 
                   MultipleActiveResultSets=True;
                   AttachDbFilename=|DataDirectory|Forms.mdf"
 providerName="System.Data.SqlClient"

FormsContext.cs:

namespace ApplicationForm.Models
{
    public class FormsContext : DbContext
    {
        public FormsContext() : base("name=FormsContext")
        {
        }

        public System.Data.Entity.DbSet<ApplicationForm.Models.Form> Forms { get; set; }
...

The database is like this:

 FormsContext->Forms->{id,uuid,first,last...}

I am not so sure what happened this time. Would anyone help me to go through this?

Edit/Resolution

I wasn't sure how to check Inner Exception and I did some research on this. So when I read the error message in the Inner Exception, I could solve my problem.

I found out what the bug was. The query field is 'firstname' but my table column name is 'first'. They did not match. Once I changed the DB column name, it was working again.

解决方案

found out what the bug was. The query field is 'firstname' but my table column name is 'first'. They did not match. Once I changed the DB column name, it was working again.

One must be diligent to keep each in sync if the database is always in flux.

Why?

EF is simply mappings, whether to tables or to sprocs and any subtle change to column names or foreign key constraints can have ripple effects.

Those effects can create obvious, like you found, errors or non obvious logical errors which can provide working (compiled) software with subtle logic bugs. I have seen the logic bugs by experience and they do happen.

So always be wary to keep the EF mappings in sync.


I answered to a different EF question but in a similar vein.

I had run into a situation where in EF a mapped stored proc at runtime failed because an internal change in the stored proc saw that a sql cast had stripped out a column name from the result, and EF (mapped before the changed) failed. I have a screen shot in my answer which shows that EF/db changes can have a deleterious effect:

The data reader is incompatible . A member does not have a corresponding column in the data reader with the same name

这篇关于System.Data.Entity.Core.EntityCommandExecutionException发生在MVC应用程序中使用EF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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