问题
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'."}
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.
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.
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?
回答1:
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.
来源:https://stackoverflow.com/questions/9490484/invalid-column-name-when-trying-to-add-an-entity-to-a-database-using-dbcontext