2012-08-15 89 views
0

我刚刚设置关于从Entity Framework 4.3.1和.NET 4更新项目到实体框架5.0和.NET 4.5。我首先更新了.NET版本,并确保我引用EF 5.0.0.0,而不是与.NET 4兼容的4.4.0.0。实体框架5生成SQL引用NotMapped属性

我有一类结构类似

public class MyBase 
{ 
    [NotMapped] 
    public bool MyProperty { get; set; } 
} 

public class MyDefinition : MyBase 
{ 
    // Some other properties 
} 

当我尝试加载一些MyDefinition情况下

using (MyContext ctx = new MyContext()) 
{ 
    ctx.Configuration.AutoDetectChangesEnabled = false; 
    ctx.Configuration.LazyLoadingEnabled = false; 
    ctx.Configuration.ProxyCreationEnabled = false; 

    var defs = from def in ctx.MyDefinitions.AsNoTracking() select def; 

    foreach (MyDefinition def in defs) // <-- Exception here 
    { 
     // Do stuff 
    } 
} 

我得到一个SQLEXCEPTION

无效的列名称myProperty的“。

这是因为如果NotMapped尊重用于确定现有的模式是否是有效的用途,但通过EF 5产生的SELECT期望有是一个myProperty的柱。

基类和派生类在不同的程序集中定义。仔细检查这两个程序集以确保它们引用EF 5.0.0.0和目标.NET 4.5。

智能感知声称NotMappedSystem.ComponentModel.DataAnnotations.Schema.NotMapped

如何防止EF 5的选择是不存在的列?

回答

0

D'oh!

我今天也更新到VS 2012。不相关的东西违背了构建后事件,这导致包含基类的程序集的早期版本可用于派生类。修复后构建事件解决了问题。

+0

嘿埃里克,只是想确认这意味着你不再看到与NotMapped的任何问题? – 2012-08-16 16:50:39

+0

@RowanMiller:这是正确的。由于构建问题,我的代码引用了旧版本的DLL,它仍然在错误的名称空间中引用了NotMapped。 – 2012-08-16 17:30:43

1

添加此

using System.ComponentModel.DataAnnotations.Schema