Entity Framework 4.3. Invalid column name 'CreatedOn'
Asked Answered
S

4

17

I'm developing an ASP.NET MVC 4 application using VS 2010 and EF 4.3. It retrieves some data from an external database and all worked as expected until I tried to recompile it one day. After the compilation I receive the following EF error:

Invalid column name 'CreatedOn'.

No DB or code changes were made - I've simply added some indentations for readability. The previous application versions from TFS also throw the same exception.

I have no CreatedOn property in my entities and no such field in the database and I don't need it and don't want it in any case.

What should be done to avoid this exception?

This is my custom DB context I use to access data:

public class MyContext<T> : DbContext where T : class, IDataEntity
{
   public MyContext(string connectionKey)
        : base("name=" + connectionKey)
    {
        Configuration.AutoDetectChangesEnabled = false;            
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Label>().Property(item => item.Id).HasColumnName("LabelId");
        modelBuilder.Entity<Label>().Ignore(item => item.ChangedBy);
    }
}

And this is the Label class

public class BaseEntity : IDataEntity 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string ChangedBy { get; set; } 
} 

public class Label : BaseEntity 
{ 
}
Selfpossessed answered 30/8, 2012 at 9:7 Comment(3)
Can you post the Label class?Laud
This is the Label class: public class BaseEntity : IDataEntity { public int Id { get; set; } public string Name { get; set; } public string ChangedBy { get; set; } } public class Label : BaseEntity { }Selfpossessed
This issue is also discussed here : #19647159Jeanninejeans
S
6

Found an answer for my question. Thanks all for replies.

Database.SetInitializer<MyContext<Label>>(null);

This fixes the problem and disables DB changes tracking in EF.

Selfpossessed answered 30/8, 2012 at 12:45 Comment(1)
Thanks this was useful, however I'm adopting this only as a temporary fix for debugging as I am not sure I want to disable DB change tracking forever... (see my confusion)Erosive
A
14

In my case it was the MiniProfiler. I use EF 5.0, it uses EF 4.x. After disabling the profiler, the exception was not thrown any more

Amie answered 24/1, 2013 at 12:45 Comment(0)
A
10

EF 4.3.1 added a CreatedOn column to the __MigrationHistory table, which EF 5.0 subsequently removed. I suspect you have upgraded EF to 4.3.1 since you last updated the database.

You could either run a Migration to add the CreatedOn column, manually add it yourself, or upgrade to EF 5.0 where it is no longer necessary.

Algia answered 30/8, 2012 at 9:35 Comment(4)
DB wasn't changed in any way. I have no "_migrationHistory" table in DB and I don't have permissions to change something in this DB. I'm working with .net framework 4.0 and when trying to upgrade to EF 5 with NuGet receive schema related exceptions: Schema specified is not valid. Errors: (0,0) : warning 0005: Could not find schema information for the attribute 'Namespace'. (0,0) : warning 0005: Could not find schema information for the attribute 'Provider'. (0,0) : warning 0005: Could not find schema information for the attribute 'ProviderManifestToken'...Selfpossessed
You'll find the __MigrationHistory table under 'System Tables'. I'm afraid I have no idea what the errors are when upgrading to EF 5.0, but technically it should work on .NET 4.0Algia
This doesn't make sense because I get this error and I never touched EF5 or EF4. I'm only using EF6 :X.Cheliform
This answer was only guaranteed to make sense at the time it was written, which was well before EF 6 was invented.Algia
S
6

Found an answer for my question. Thanks all for replies.

Database.SetInitializer<MyContext<Label>>(null);

This fixes the problem and disables DB changes tracking in EF.

Selfpossessed answered 30/8, 2012 at 12:45 Comment(1)
Thanks this was useful, however I'm adopting this only as a temporary fix for debugging as I am not sure I want to disable DB change tracking forever... (see my confusion)Erosive
T
1

Actually there is CreatedOn column in __MigrationHistory table so the problem might be something EF migrations related. If there are no codechanges,pending migrations or anything the error is indeed a very strange case.

PS. I found this, it might prove helpful: http://bstechnical.blogspot.fi/2012/08/invalid-column-name-createdon-code-first.html

Tommietommy answered 30/8, 2012 at 9:43 Comment(1)
As you can see from my custom DB context I already put Configuration.AutoDetectChangesEnabled = false, but this doesn't help. Is there any other way to disable db changes detection in EF?Selfpossessed

© 2022 - 2024 — McMap. All rights reserved.