title: Handle one-to-many relationships between business logic and data access layer
body: <p>I have the following <strong>architecture</strong>:</p>
  
  <ul>
  <li><em>Business logic</em>: Business logic happens here. This layer deals with <em>data transform objects</em> only (= objects containing business related data, no technical data)</li>
  <li><em>Data access layer</em>: Provides connection to the database. Accepts <em>data transform objects</em> and transforms them into <em>entity objects</em> (= objects containing database related information like technical ids). Returns <em>data transform objects</em>.</li>
  <li>Some other layers (not important)</li>
  </ul>
  
  <p>Now I'm dealing with one-to-many relationships (example: an invoice can have multiple positions).</p>
  
  <p><strong>Data transform objects</strong>:</p>
  
  <pre><code>public class InvoiceDto
  {
      public string Number { get; set; }
      public DateTime CreationDate { get; set; }
      public List&lt;InvoicePositionDto&gt; InvoicePositions { get; set; }
  }
  
  public class InvoicePositionDto
  {
      public string Description { get; set; }
      public decimal Costs { get; set; }
  }
  </code></pre>
  
  <p><strong>Entity objects</strong>:</p>
  
  <pre><code>[Table("Invoices")]
  public class InvoiceEntity : BaseEntity
  {
      [Required]
      public string Number { get; set; }
      [Required]
      public DateTime CreationDate { get; set; }
  
      public virtual ICollection&lt;InvoicePositionEntity&gt; InvoicePositions { get; set; }
  }
  
  [Table("InvoicePosition")]
  public class InvoicePositionEntity : BaseEntity
  {
      [Required]
      public string Description { get; set; }
      [Required]
      public decimal Costs { get; set; }
  
      [Required]
      public int InvoiceId { get; set; }
      public virtual InvoiceEntity Invoice { get; set; }
  }
  </code></pre>
  
  <hr>
  
  <p>When creating an invoice, I will just insert all positions on <em>data access layer</em>.</p>
  
  <p>When updating an invoice, I have to consider the following cases regarding the positions of an invoice:</p>
  
  <ol>
  <li>Nothing was changed</li>
  <li>A new position was added</li>
  <li>A position was deleted</li>
  <li>An existing position was changed</li>
  </ol>
  
  <p>How can I handle those cases? I guess I always have to "compare" the <code>InvoicePositionDtos</code> with the <code>InvoicePositionEntities</code> of the existing dataset. Is there an easier way?</p>
  
  <p>Technical details - the following frameworks are used:</p>
  
  <ul>
  <li>Entity Framework</li>
  <li>AutoMapper</li>
  </ul>