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
{
}
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With