【发布时间】:2012-08-24 23:31:19
【问题描述】:
我正在使用 VS 2010 和 EF 4.3 开发一个 ASP.NET MVC 4 应用程序。它从外部数据库中检索一些数据,并按预期工作,直到有一天我尝试重新编译它。编译后我收到以下 EF 错误:
列名“CreatedOn”无效。
没有进行数据库或代码更改 - 我只是添加了一些缩进以提高可读性。来自 TFS 的先前应用程序版本也会引发相同的异常。
我的实体中没有CreatedOn 属性,数据库中也没有这样的字段,我不需要它,无论如何也不想要它。
应该怎么做才能避免这个异常?
这是我用来访问数据的自定义数据库上下文:
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);
}
}
这是标签类
public class BaseEntity : IDataEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string ChangedBy { get; set; }
}
public class Label : BaseEntity
{
}
【问题讨论】:
-
你能发布
Label类吗? -
这是标签类: public class BaseEntity : IDataEntity { public int Id { get;放; } 公共字符串名称 { 获取;放; } 公共字符串 ChangedBy { get;放; } } 公共类标签 : BaseEntity { }
-
这里也讨论了这个问题:stackoverflow.com/questions/19646659/…
标签: asp.net-mvc entity-framework entity-framework-4