title: Entity Framework Code First Optional One to Many Relationship on implied Composite Key
body: <p><strong>Update</strong>: Created a <a href="https://github.com/TheJeremyGray/ConsoleApplication7" rel="nofollow">sample project</a> on GitHub.  </p>
  
  <p>In my existing database I have a generic audit table that has no keys, but I'd like to insert into this audit table with EF code first. </p>
  
  <p>Existing reports pull audits based on AffectedId and EntityId where EntityId is hardcoded in a bunch of places.<br>
  I reversed engineered the database, but there is no explicit relationship between these tables...so here are the base POCO objects (I can't make a relationship either, its an existing system)</p>
  
  <pre><code>public class Audit
  {
      public int Id { get; set; }
      public int EntityId { get; set; }
      public string AffectedId { get; set; }       
      public string NewValue { get; set; }
  }
  
  public class Action1
  {
      public int Id { get; set; }
      public string Desc { get; set; }
  }   
  
  public class Action2
  {
      public int Id { get; set; }
      public string Desc { get; set; }
  }
  </code></pre>
  
  <p>I think I want the POCO objects to look like this</p>
  
  <pre><code>public class Audit
  {
      public int Id { get; set; }
      public int EntityId { get; set; }
      public string AffectedId { get; set; }       
      public string NewValue { get; set; }
      public virtual Action1 Action1 { get; set; } // but I don't want this to change audit table
      public virtual Action2 Action2 { get; set; } // but I don't want this to change audit table
  }
  
  public class Action1
  {
      public Action1() {this.Audits = new List&lt;Audit&gt;();}
      public int Id { get; set; }
      public string Desc { get; set; }
      public virtual ICollection&lt;Audit&gt; Audits { get; set; }
  }   
  
  public class Action2
  {
      public Action2() {this.Audits = new List&lt;Audit&gt;();}
      public int Id { get; set; }
      public string Desc { get; set; }
      public virtual ICollection&lt;Audit&gt; Audits { get; set; }
  }
  </code></pre>
  
  <p>But I can't seem to get the fluent mapping to allow me to insert an Action (1 or 2) that has populated AffectedId's in the Audit.  This is what I was thinking on the mapping objects, but can't seem to get the hardcoded EntityId key to work correctly. </p>
  
  <pre><code>public class AuditMap : EntityTypeConfiguration&lt;Audit&gt;
  {
      public AuditMap()
      {
          // Primary Key
          this.HasKey(t =&gt; t.Id);
  
  
          this.HasOptional(t =&gt; t.Action1)
              .WithMany(t =&gt; t.Audits)
              .HasForeignKey(t =&gt; new {EntityId = 3, AffectedId = t.Id});
  
          this.HasOptional(t =&gt; t.Action2)
              .WithMany(t =&gt; t.Audits)
              .HasForeignKey(t =&gt; new {EntityId = 5, AffectedId = t.Id});
      }
  }
  
  public class Action1 : EntityTypeConfiguration&lt;Action1&gt;
  {
      public Action1Map()
      {
          // Primary Key
          this.HasKey(t =&gt; t.Id);
      }
  }
  
  public class Action2 : EntityTypeConfiguration&lt;Action2&gt;
  {
      public Action2Map()
      {
          // Primary Key
          this.HasKey(t =&gt; t.Id);
      }
  }
  </code></pre>
  
  <p>Any suggestions on how to change the C# and not the SQL would be appreciated.  </p>