尝试使用DbContext将实体添加到数据库时列名无效 [英] Invalid column name when trying to add an entity to a database using DbContext

查看:302
本文介绍了尝试使用DbContext将实体添加到数据库时列名无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码将实体添加到我的数据库中:

I'm trying to add an entity to my database with the following code:

public void StoreElectronicSignatureType(ElectronicSignatureTypeModel model)
{
    [...]
    ElectronicSignatureType electronicSignatureType = new ElectronicSignatureType();

    Entity entity =  GetEntity("Test");
    electronicSignatureType.Entity = entity;

    Add(electronicSignatureType);

public void Add(object entity)
{
   DbContext.Entry(entity).State = System.Data.EntityState.Added;
   //DbContext.Set(entity.GetType()).Add (entity);
   DbContext.SaveChanges();
}

当我这样做时,我会收到以下错误:

When I do this I get the following error:

{"Invalid column name 'Custom.MonsterBehandeling'."}   

我可以找到这个错误的例子是关于尝试从数据库中选择他们尝试选择的列不存在的数据,所以我以为我在尝试插入列 Custom.MonsterBehandeling 中没有该列存在。实际上,在数据库中查看测试表没有Custom.MonsterBehandeling。

The examples of this error that I can find are all about trying to select data from a database where the column that they try to select doesn't exist, so I thought that I'm trying to insert in column Custom.MonsterBehandeling without that column existing. Indeed, looking in the database the "Test" table does not have a Custom.MonsterBehandeling.

但是,实体 Test 不包含 Custom MonsterBehandeling Custom.MonsterBehandeling 。在整个解决方案中搜索 MonsterBehandeling 仅提供两个命中:

However, the entity Test does not contain either Custom, MonsterBehandeling or Custom.MonsterBehandeling. Searching for MonsterBehandeling in the whole solution only gives two hits:

<CustomTable Name="Sample">
   <Columns>
 <ColumnDefinition IsUnique="false" IsDiscriminator="false" IsIdentity="false" Description="Monster behandeling" Size="0" Precision="0" Scale="0" DataType="Text" Name="MonsterBehandeling" IsPrimaryKey="false" AllowNull="true" />
    </Columns>
</CustomTable>    <field name="ItemExpression" value="Sample.Custom.MonsterBehandeling"/>

在我没有使用的数据库模式中,删除它仍然会产生相同的错误,而

In a database schema that I'm not using, removing it still gives the same error, and

<field name="ItemExpression" value="Sample.Custom.MonsterBehandeling"/>

也删除这个我仍然得到相同的错误。

in a configuration.xml file. Also after removing this I still get the same error.

看到我甚至不能在数据库或我的解决方案中找到 MonsterBehandeling ,我不知道在哪里开始寻找解决方案。另外,我不知道为什么我认为我得到这个错误是正确的。那么,什么原因导致一个 {无效的列名称...。} 插入数据时出错,我该怎么办来解决这个问题?

Seeing that I can't even find MonsterBehandeling in the database or in my solution, I have no idea where to start looking for a solution. Also, I'm not sure if why I think that I get this error is correct. So, what causes an {"Invalid column name '...'."} error when inserting data, and what can I do to solve this?

推荐答案

我遇到同样的问题,花了几个小时修复它。事实证明,在我的情况下,实体框架edmx文件是针对与运行SaveChanges()的数据库模式不同的数据库模式构建的。由于您提到第二个模式,这听起来与您的场景相似。
我的线索是错误是一个常见的SQL Server错误(不是c#一),表示不好的sql。
第1步是运行SQL Profiler,假设您正在使用SQL Server或类似的工具来捕获违规SQL,这是一个带有缺少/已删除列的INSERT语句。
步骤2是在文本编辑器中打开edmx文件,然后找到违规列。通过Visual Studio进行的全局搜索以及F3搜索没有列出列的名称。
一旦与代码相同的数据库模式构建了edmx文件(所以一旦我的所有连接字符串正确和同步),一切正常。希望这有帮助。

I experienced the same problem and spent a few hours fixing it. It turns out in my case that the Entity Framework edmx file was built against a different database schema than the one against which the SaveChanges() was run. This sounds potentially similar to your scenario since you mentioned a second schema. The clue to me was that the error is a common SQL Server error (not a c# one), indicating bad sql. Step 1 was to run SQL Profiler, assuming you are using SQL Server, or similar tool, to trap the offending SQL, which was an INSERT statement with the missing/deleted column. Step 2 was to open the edmx file in a text editor to then find the offending column. A global search via Visual Studio as well as F3 search did not turn up the name of the column. Once the edmx file was built against the same database schema as the code (so once I got all my connection strings correct and in sync), everything worked. Hope this helps.

这篇关于尝试使用DbContext将实体添加到数据库时列名无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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