次のオブジェクトのセットがあります。
public class Form
{
public int FormId { get; set; }
public DateTime DateCreatedOn { get; set; }
public string Status { get; set; }
}
// This is using TPT inheritance from Form
[Table("FormA")]
public class FormA : Form
{
public string ExtraInfoA { get; set; }
public virtual ICollection<Child> Children
}
// This is using TPT inheritance from Form
[Table("FormB")]
public class FormB : Form
{
public string ExtraInfoB { get; set; }
public virtual ICollection<Adult> Adults
}
public class Person
{
public int PersonId { get; set; }
public int FormId
public DateTime DateOfBirth { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
// This is using TPH inheritance from Person
public class Adult : Person
{
public int HowManyCars { get; set; }
public string NationalInsuranceNo { get; set; }
[ForeignKey("FormId")]
public virtual FormB FormB { get; set; }
}
// This is using TPH inheritance from Person
public class Child : Person
{
public int HowManyToys { get; set; }
public string SchoolName { get; set; }
[ForeignKey("FormId")]
public virtual FormA FormA { get; set; }
}
Form
これにより、フォーム、FormA
、およびの 3 つのテーブルが作成されFormB
、すべてに適切なフィールドが含まれます。また、 のテーブルを 1 つ作成しますPerson
。
ForeignKey
問題は、規則に依存して属性を指定しない場合、Person
テーブルに 2 つの追加の外部キー列が含まれることです。
ただし、ForeignKey
(上記のコードのように) 属性を指定すると、次のエラー メッセージが表示されます。
`The foreign key component 'FormId' is not a declared property on type 'Child'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.`
FormId
は間違いなくのプロパティであるChild
ため、何が問題なのかわかりません。
私たちの実世界の状況は上記の状況よりもはるかに複雑なので、複数の外部キーを使用するよりも、今すぐ取得したいと思います。
どんな助けでも大歓迎です。