I'm trying to convert the following model (see image below) to Code First. I've tried various combinations involving ForeignKey and InverseProperty attributes with no luck. I've found this answer but it seems that combinations of ForeignKey and InverseProperty cause a different behaviour.
The attached source code gives the following error:
Unable to determine the principal end of an association between the types 'InversePropertyTest.Author' and 'InversePropertyTest.Book'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
This is my model with EDMX
Sample code:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InversePropertyTest
{
public class Author
{
public int AuthorId { get; set; }
public Nullable<int> CurrentlyWorkingBookId { get; set; }
[InverseProperty("Author")] public ICollection<Book> Books { get; set; }
[ForeignKey("CurrentlyWorkingBookId"), InverseProperty("EditoredBy")] public Book CurrentlyWorkingBook { get; set; }
}
public class Book
{
public int BookId { get; set; }
public int AuthorId { get; set; }
[ForeignKey("AuthorId"), InverseProperty("Books")] public Author Author { get; set; }
[InverseProperty("CurrentlyWorkingBook")] public Author EditoredBy { get; set; }
}
public class SimpleContext : DbContext
{
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
class Program
{
static void Main(string[] args)
{
using (var context = new SimpleContext())
{
IList<Author> authors = (from a in context.Authors select a).ToList();
IList<Book> books = (from b in context.Books select b).ToList();
}
}
}
}
Any help is greatly appreciated