Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>FINAL SOLUTION</strong></p> <p>Thanks to your reference to InverseProperty I found an amazing article that covers exactly what I wanted to accomplish using fluent API. This article was written in January, but I'm pretty sure CTP5 is now officially part of the MVC 3 and EF core.</p> <p><a href="http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx" rel="nofollow">Associations in EF Code First CTP5</a></p> <p>Okay...I'm going to document what I found to work great! I hate it when people leave partial answers, so here we go.</p> <p>There is a little redundancy here, but it works. My Quote Class looks like this:</p> <pre><code> [ForeignKey("Creator")] public virtual int CreatorID { get; set; } [InverseProperty("CreatedQuotes")] public virtual User Creator { get; set; } [ForeignKey("Agent")] public virtual int AgentID { get; set; } [InverseProperty("OwnedQuotes")] public virtual User Agent { get; set; } [ForeignKey("Manager")] public virtual int ManagerID { get; set; } [InverseProperty("ManagedQuotes")] public virtual User Manager { get; set; } </code></pre> <p>Then my User class looks like this:</p> <pre><code> public virtual ICollection&lt;Quote&gt; CreatedQuotes { get; set; } public virtual ICollection&lt;Quote&gt; OwnedQuotes { get; set; } public virtual ICollection&lt;Quote&gt; ManagedQuotes { get; set; } </code></pre> <p>Finally, my DBContext class looks like this:</p> <pre><code> protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;(); modelBuilder.Entity&lt;Quote&gt;() .HasRequired(a =&gt; a.Manager) .WithMany() .HasForeignKey(u =&gt; u.ManagerID); modelBuilder.Entity&lt;Quote&gt;() .HasRequired(a =&gt; a.Agent) .WithMany() .HasForeignKey(u =&gt; u.AgentID).WillCascadeOnDelete(false); modelBuilder.Entity&lt;Quote&gt;() .HasRequired(a =&gt; a.Manager) .WithMany() .HasForeignKey(u =&gt; u.ManagerID).WillCascadeOnDelete(false); } </code></pre> <p>You can see the redundancy in the ForeignKey annotation in the Quote class and the Fluent API mapping in the DbContext class, but it's not hurting anything. I could probably do away with the annotations in the Quote class, but the Fluent API is necessary to set the cascading rule to false to prevent foreign key conflicts.</p> <p>I have been able to navigate both directions with no problems and exactly as expected.</p> <p>Thanks for all your help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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