Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In my opinion this should work:</p> <pre><code>public class Photo { [Key, ForeignKey("Parent"), Column(Order = 1)] public int PhotoCollectionId { get; set; } [Key, Column(Order = 2)] public int PhotoId { get; set; } public virtual PhotoCollection Parent { get; set; } public virtual ISet&lt;PhotoProperty&gt; PhotoProperties { get; private set; } //... } public class PhotoProperty { [Key, ForeignKey("Parent"), Column(Order = 1)] public int PhotoCollectionId { get; set; } [Key, ForeignKey("Parent"), Column(Order = 2)] public int PhotoId { get; set; } [Key, Column(Order = 3)] public int PhotoPropertyId { get; set; } public virtual Photo Parent { get; set; } //... } </code></pre> <p>Note that <code>PhotoCollectionId</code> in <code>PhotoProperty</code> doesn't refer to a <code>PhotoCollection</code> but is part of the composite foreign key <code>(PhotoCollectionId,PhotoId)</code> that refers to <code>Photo</code>.</p> <p>And yes, you can define the whole mapping with Fluent API:</p> <pre><code>modelBuilder.Entity&lt;PhotoCollection&gt;() .HasMany(pc =&gt; pc.Photos) .WithRequired(p =&gt; p.Parent) .HasForeignKey(p =&gt; p.PhotoCollectionId); modelBuilder.Entity&lt;Photo&gt;() .HasKey(p =&gt; new { p.PhotoCollectionId, p.PhotoId }); modelBuilder.Entity&lt;Photo&gt;() .Property(p =&gt; p.PhotoId) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); modelBuilder.Entity&lt;Photo&gt;() .HasMany(p =&gt; p.PhotoProperties) .WithRequired(pp =&gt; pp.Parent) .HasForeignKey(pp =&gt; new { pp.PhotoCollectionId, pp.PhotoId }); modelBuilder.Entity&lt;PhotoProperty&gt;() .HasKey(pp =&gt; new { pp.PhotoCollectionId, pp.PhotoId, pp.PhotoPropertyId }); modelBuilder.Entity&lt;PhotoProperty&gt;() .Property(pp =&gt; pp.PhotoPropertyId) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    3. VO
      singulars
      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