0

屬性我從遷移實體框架2011年6月CTP到實體Frameowrk 5(.NET 4.5)的應用程序。我刪除了2011年6月CTP的所有EF引用,並在Visual Studio 2012中爲EF 5添加了這些引用。修復了一些命名空間錯誤後,應用程序編譯正常。但是當我嘗試運行應用程序並訪問數據時,我遇到了異常。由於NotMapped Attribute,我在我的基礎實體類中發生異常。這裏是相關的實體(Base和Derived)。遷移到的EntityFramework 5 - NotMapped在基類中引發異常

基地實體類

[Table("Users")] 
[Serializable] 
public abstract class User { 
    [Key] 
    public long Id { get; set; } 

    // Other Properties omitted 

    [NotMapped] 
    public string StringVersion { 

    } 
}   

派生實體類

[Table("Donors")] 
[Serializable] 
public class Donor : User { 
    ... 
} 

當應用程序試圖訪問數據,引發InvalidOperationException以下消息

You cannot use Ignore method on the property 'StringVersion' on type 'Donor' because 
this type inherits from the type 'User' where this property is mapped. To exclude 
this property from your model, use NotMappedAttribute or Ignore method on the base type. 

我試圖根據http://entityframework.codeplex.com/workitem/481中描述的解決方法來解決問題,但異常仍在拋出。具體來說,我使用了以下代碼,以便在捐助實體之前發現用戶。

public class DonorContext : DbContext { 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) { 
     //Change for EF 5 
     modelBuilder.Entity<User>(); 
     // 

     //Other Fluent API code follows 
    } 
} 

我該如何解決這種情況?

回答

1

我能得到它通過註釋NotMapped在用戶(基地)屬性實體的工作,而是使用流利的API忽略爲如下。

public class DonorContext : DbContext { 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) { 

     //Added for EF 5 
     modelBuilder.Entity<User>().Ignore(u => u.StringVersion); 
    } 
} 

由於對於繼承實體我只有幾個NotMapped屬性,所以我可以避開上述解決方法。我希望忽略行爲與NotMapped相同,並使用其中一個代替其他行爲不會造成任何問題。