Note that there are some explanatory texts on larger screens.

plurals
  1. POEF5 InverseProperty Issue
    text
    copied!<p>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 <a href="https://stackoverflow.com/questions/5716528/entity-framework-4-1-inverseproperty-attribute">answer</a> but it seems that combinations of ForeignKey and InverseProperty cause a different behaviour.</p> <p>The attached source code gives the following error:</p> <blockquote> <p>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.</p> </blockquote> <p>This is my model with EDMX</p> <p><img src="https://i.stack.imgur.com/dYyEk.png" alt="Model example"></p> <p>Sample code:</p> <pre><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&lt;int&gt; CurrentlyWorkingBookId { get; set; } [InverseProperty("Author")] public ICollection&lt;Book&gt; 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&lt;Author&gt; Authors { get; set; } public DbSet&lt;Book&gt; Books { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { } } class Program { static void Main(string[] args) { using (var context = new SimpleContext()) { IList&lt;Author&gt; authors = (from a in context.Authors select a).ToList(); IList&lt;Book&gt; books = (from b in context.Books select b).ToList(); } } } } </code></pre> <p>Any help is greatly appreciated</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload