title: How to define nested Identifying Relationships Entity Framework code first
body: <p>I'm using EF5 code first in a simple test application at the moment to test various functions. I have defined an 'identifying relationship' between two entities which represent a one-to-many link. Here I define a PhotoCollection that has many child Photo entities;</p>
  
  <pre><code>public class PhotoCollection
  {
      public int Id { get; set; }
      public virtual ISet&lt;Photo&gt; Photos { get; private set; }
  
      public PhotoCollection()
      {
          Photos = new HashSet&lt;Photo&gt;();
      }
  }
  
  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; }
  
  
      [Required, MaxLength(200)]
      public string FilePath { get; set; }
  
      public Photo()
      {
      }
  
  }
  </code></pre>
  
  <p>and my implementation of OnModelCreating includes;</p>
  
  <pre><code>modelBuilder.Entity&lt;Photo&gt;().Property(p =&gt; p.PhotoId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  </code></pre>
  
  <p>The result is that when I delete a PhotoCollection, all the photo entities are deleted as well which is provided by the 'identifying relationship'. Great.</p>
  
  <p><strong>My question is how do I define a further level in this object graph</strong>, let's say I want to a PhotoProperties as a one-to-many collection from Photo. In this case I'd like to delete PhotoCollection and the all appropriate Photo and PhotoProperty records will be deleted too. Using the approach above, wouldn't it be necessary to add a GrandParent property to PhotoProperty that pointed to the PhotoCollection?</p>
  
  <p>Can I achieve the same result using the fluent Api in model builder?</p>
  
  <p>The only examples I've managed to find online are for a single level parent>child hierarchies.</p>
  
  <p>Thanks in advance.</p>