问题描述
我收到错误:
连接字符串到DB:
connectionString =Data Source =(localdb)\v11.0;
初始目录= FormsContext;
集成安全= True;
MultipleActiveResultSets = True;
AttachDbFilename = | DataDirectory | Forms.mdf
providerName =System.Data.SqlClient
FormsContext.cs:
命名空间ApplicationForm.Models
{
public class FormsContext: DbContext
{
public FormsContext():base(name = FormsContext)
{
}
public System.Data.Entity.DbSet< ApplicationForm.Models.Form>表格{get;组;
...
数据库是这样的:
FormsContext-> Forms-> {id,uuid,first,last ...}
pre>
我不太确定这次发生了什么事。有人会帮我办理这个吗?
编辑/解决方案
不知道如何检查内在异常,我做了一些研究。所以当我读到内部异常中的错误消息时,我可以解决我的问题。
我发现了什么是错误。查询字段是firstname,但是我的表列名称是first。他们不匹配一旦我更改了DB列的名称,它再次工作。
解决方案如果数据库总是,必须努力保持每个同步通量。
为什么?
EF只是映射,无论是表还是sprocs,任何微妙的变化s)对列名或外键约束可以使旧的映射到新结构中的纹理效果;因此奇怪的行为。
这些效果可以创建明显的,如您发现的错误或非明显的逻辑错误,可以为工作(编译)软件提供微妙的逻辑错误。
因此,始终保持EF映射同步。
>
我回答了一个不同的EF问题,但是类似的。
我遇到了一个情况,在EF中映射的存储过程在运行时失败,因为存储过程中的内部更改看到一个sql
cast
从结果中删除了列名,EF(在更改前映射)失败。我的答案中有一个屏幕截图,显示EF / db更改可能会产生有害影响:
I am about to create an Application Form with UUID as its unique key (not primary key).
I got the error:
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.
解决方案One must be diligent to keep each in sync if the database is always in flux.
Why?
EF is simply mappings, whether that is to tables or to sprocs, any subtle change(s) to column names or foreign key constraints can have ripple effects for old mappings into new structures; 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:这篇关于System.Data.Entity.Core.EntityCommandExecutionException在MVC应用程序中发生使用EF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!