System.Data.Entity.Core.EntityCommandExecutionException Occurred in MVC app Using EF

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

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

回答1:

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 mapping to tables or stored procedures. Any subtle change(s) to column names or foreign key constraints can have ripple effects for old mappings into new structure(s); hence weird behaviors.

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



回答2:

In my case helped next:

1) press View Detail.. button:

2) Check your actual error:

Hope it helps for someone.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!