これは私の(単純化された)問題です。 クラス:Entity Framework 4.1の派生クラスの他のデータベースフィールドを持たないTPH
public class RetailExposure
{
public int RetailExposureId { get; set; }
public int RetailModelId { get; set; }
}
public class PocRetailExposure : RetailExposure
{
[NotMapped]
public string IncidentScore { get; set; }
}
と私のOnModelCreatingコード:
System.Data.MappingException:
(6,10) : error 3032: Problem in mapping fragments starting at line 6:Condition
member 'RetailExposure.RetailModelId' with a condition other than 'IsNull=False'
is mapped. Either remove the condition on RetailExposure.RetailModelId or remove
it from the mapping.
:
context.RetailExposures.Add(new RetailExposure { RetailModelId = 1 });
context.SaveChanges();
は、私は次のエラーを取得する:しかし
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<RetailExposure>().ToTable("dbo.RetailExposure");
modelBuilder.Entity<RetailExposure>().Map<PocRetailExposure>(p => p.Requires("RetailModelId").HasValue(1).IsRequired());
}
私は新しい露出レコードを追加するとき
私が試していることRetailModelIdフィールドから決定された派生クラスを持つ基本クラス 'RetailExposure'があります。派生クラスには、データベースに存在するプロパティは含まれません。私はdiscoriminatorとしてRetailModelIdを使用するようにEFを設定する方法を考えたようですが、変更を保存するときにもこのエラーが発生します。
これを動作させるにはコンテキストをどのように設定する必要がありますか?それとも、EFが現在サポートしていないことをやろうとしていますか?
私は、派生したクラスを完全に無視するようにEFに指示することができますが、これは私が欲しいものではありません。
ああ。もちろん、クラス内のプロパティに識別子をマッピングすることはできません。そうすることができれば、それほどオブジェクト指向ではありません。それを修正し、RetailExposure抽象を作成しました。テストは合格。わーい!ありがとう –