实体框架4.3。无效的列名称CreatedOn“ [英] Entity Framework 4.3. Invalid column name 'CreatedOn'

查看:549
本文介绍了实体框架4.3。无效的列名称CreatedOn“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用VS 2010和EF 4.3的ASP.NET MVC应用4。它从外部数据库中检索一些数据,所有的预期一样,直到我试图重新编译一天。编译后,我收到以下错误EF:


  

无效的列名称CreatedOn。


没有DB或code进行了更改 - 我只是添加了一些凹陷的可读性。从TFS的previous应用程序版本也抛出同样的异常。

我在实体没有 CreatedOn 属性,并在数据库中没有这样的领域,我并不需要它,不希望它在任何情况下。

应该怎样做,以避免这种例外?

这是我的自定义数据库方面,我用它来访问数据:

 公共类MyContext< T> :的DbContext其中T:类,IDataEntity
{
   公共MyContext(字符串connectionKey)
        :基地(NAME =+ connectionKey)
    {
        Configuration.AutoDetectChangesEnabled = FALSE;
    }    保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
    {
        modelBuilder.Entity<标签>()房产(项目=> item.Id)。.HasColumnName(LabelId);
        modelBuilder.Entity<标签>()忽略(项目=> item.ChangedBy);
    }
}

这是Label类

 公共类BaseEntity:IDataEntity
{
    公众诠释标识{搞定;组; }
    公共字符串名称{;组; }
    公共字符串ChangedBy {搞定;组; }
}公共类标签:BaseEntity
{
}


解决方案

找到了我的问题的答案。感谢所有的答复。

  Database.SetInitializer< MyContext<标签>>(NULL);

这解决了这个问题,并禁用DB变化EF跟踪。

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 
{ 
}

解决方案

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.

这篇关于实体框架4.3。无效的列名称CreatedOn“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆