EFコードが大好きですが、たぶんSQLでテーブルを定義する方が簡単なようです。Entity Frameworkコード1つのクラスで2つの参照を別のクラスに持つ
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<BookRecommendation> BookRecommendations { get; set; }
// additional properties about the item
}
public class BookRecommendation
{
public int Id { get; set; }
// public int BookId { get; set; } // from attempts to use data annotations
public virtual Book Book { get; set; }
// public int BookRecommendedId { get; set; } // from attempts to use data annotations
public virtual Book BookRecommended { get; set; }
// additional properties about the relationship between the two items
}
残念ながら、私は、これはデータの注釈や流暢APIのいずれかで正しく動作させることはできません。この場合
は、私は2つのモデル、このようなものを持っています。Multiple foreign keys pointing to same table in Entity Framework 4.1 code first、Entity Framework 4.1 InverseProperty Attribute and ForeignKeyなどの質問がありますが、これらは両方の側でコレクションを含む傾向があります。フラストレーションが早い内に設定するので
私のモデルはちょうど、間違っている可能性が、私は、私はSQLでこれを行うだろうかについて考えた:
Book
Id
Name
// other properties
Recommendation
Id
BookId
RecommendedBookId
// other properties
、その後Book.Id : Recommendation.BookId
とBook.Id : Recommendation.RecommendedBookId
間の外部キーがあるでしょう。
このアノテーションを使用するにはデータアノテーションまたはFluent APIを使用して行う必要がありますか、モデルをどのように変更する必要がありますか?
[Entity Framework Code First - 同じテーブルの2つの外部キー]の可能な複製(http://stackoverflow.com/questions/5559043/entity-framework-code-first-two-foreign-keys-from-same-テーブル) –
正直なところ、この質問が4歳以上であることを考えると、それが可能な複製かどうかは100%とは言えません。しかし、その質問に関する2つの最も高い評価された回答と、3月の13日のこの最後の回答に基づいて、例外を得ていないようですが、データベースの前提と一致しない表これらに向かって傾いている。 –
「可能な重複」は、クリーンアップする方法です。類似の質問を閉じて、最良の回答を保つ方法です。 http://meta.stackexchange.com/questions/147643/should-i-vote-to-close-a-duplicate-question-even-though-its-much-newer-and-haを参照してください。 –