I love EF Code First, but sometimes it seems it would be easier to just define the tables in SQL.
In this case I have two models, something like this:
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
}
Unfortunately, I can't get this to work correctly with either data annotations or the fluent API.
There are questions like Multiple foreign keys pointing to same table in Entity Framework 4.1 code first, Entity Framework 4.1 InverseProperty Attribute and ForeignKey, and others like this, but these tend to involve collections on both end.
My models could just be wrong, since frustration set in early and I thought about how I'd do this in SQL:
Book
Id
Name
// other properties
Recommendation
Id
BookId
RecommendedBookId
// other properties
There would then be foreign keys between Book.Id : Recommendation.BookId
and Book.Id : Recommendation.RecommendedBookId
.
What do I need to do via data annotations or fluent API to get this working, or how should I modify my models?