2012-08-15 40 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