Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4.3. Invalid column name 'CreatedOn'

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 
{ 
}
like image 446
msaruyev Avatar asked Aug 30 '12 09:08

msaruyev


3 Answers

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

like image 100
Chris Avatar answered Sep 16 '22 11:09

Chris


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.

like image 26
Patrick McDonald Avatar answered Sep 20 '22 11:09

Patrick McDonald


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.

like image 27
msaruyev Avatar answered Sep 20 '22 11:09

msaruyev