アプリケーションを Entity Framework June 2011 CTP から Entity Frameowrk 5 (.net 4.5) に移行しています。2011 年 6 月の CTP のすべての EF 参照を削除し、Visual Studio 2012 の EF 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
}
}
この状況を回避するにはどうすればよいですか?