我刚刚着手将项目从实体框架 4.3.1 和 .NET 4 更新到实体框架 5.0 和 .NET 4.5。 我首先更新了 .NET 版本,并确保引用的是 EF 5.0.0.0,而不是与 .NET 4 兼容的 4.4.0.0。
我有一个类结构,例如
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
列名称"我的属性"无效。
就好像在确定现有架构是否有效时会遵循 NotMap,但 EF 5 生成的 SELECT 需要有一个 MyProperty 列。
基类和派生类在不同的程序集中定义。 这两个程序集都经过仔细检查,以确保它们引用 EF 5.0.0.0 和目标 .NET 4.5。
Intellisense声称NotMap是System.ComponentModel.DataAnnotations.Schema.NotMapping
如何防止 EF 5 选择该不存在的列?
添加这个
using System.ComponentModel.DataAnnotations.Schema
D'oh!
我今天也更新到VS 2012。 与生成后事件无关的内容中断,该事件导致包含基类的程序集的早期版本可供派生类使用。 修复生成后事件解决了问题。