Invalid column name when trying to add an object to the database using DbContext

I am 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'."} 

Examples of this error that I can find are all attempts to select data from a database where the column they are trying to select does not exist, so I thought that I was trying to insert Custom.MonsterBehandeling into the column without this column. Indeed, looking at the database, the Test table does not have Custom.MonsterBehandeling.

However, the Test object contains neither Custom , MonsterBehandeling , nor Custom.MonsterBehandeling . Finding MonsterBehandeling in the whole solution gives only two clicks:

 <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 am not using, deleting it still gives the same error, and

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

in the configuration.xml file. Also, after uninstalling, 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 why I think I am getting this error correctly. So what causes the {"Invalid column name '...'."} Error when inserting data and what can I do to solve this problem?

0
source share
1 answer

I had the same problem and spent several hours fixing it. In my case, it turns out that the edmx Framework Entity Framework file was created against a different database schema than the one against which SaveChanges () was executed. This sounds potentially similar to your scenario as you mentioned the second scheme. The key to me was that the error is a common SQL Server error (not C # one), which indicates a bad sql. Step 1 was to start SQL Profiler if you are using SQL Server or a similar tool to catch the malicious SQL that was an INSERT statement with a missing / deleted column. Step 2 was to open the edmx file in a text editor to find the offending column. Global search through Visual Studio as well as F3 search did not display the column name. After the edmx file was created against the same database schema as the code (so when I connect all the connection strings correctly and synchronously), everything worked. Hope this helps.

+2
source

All Articles