Note that there are some explanatory texts on larger screens.

plurals
  1. POCode first TPC Inheritance with concrete base and derived, concrete types in EF
    text
    copied!<p>I'm trying to set up a TPC inheritance using Code First to model incoming and outgoing messages and the records therein. The base type, SentRecord, is concrete and its derived type, ReceivedRecord, is also concrete and inherits from SentRecord and adds a few extra fields in order to record return codes. Something like this, but with more properties:</p> <pre><code>public class SentRecord : RecordBase { public int Id { get; set; } public string FooField { get; set; } } public class ReceivedRecord : SentRecord { public int ReturnCode { get; set; } public SentRecord SentRecord { get; set; } } </code></pre> <p>The current model is TPH and as a result the tables get a descriminator column to identify the type of object that was persisted. It works, but I'd prefer both objects to be stored in separate tables, without the need of the discriminator column. The table SentRecord would only have the columns Id and FooField and the table ReceivedRecord would have Id, FooField, ReturnCode and an FK to SentRecord.</p> <p>I currently have the following in my DataContext class:</p> <pre><code>public class Context : DContext { public DbSet&lt;SentRecord&gt; SentRecords { get; set; } public DbSet&lt;ReceivedRecord&gt; ReceivedRecords { get; set; } } </code></pre> <p>And I have the following configuration for the ReceivedRecord:</p> <pre><code>public class ReceivedRecord_Configuration : EntityTypeConfiguration&lt;ReceivedRecord&gt;{ public ReceivedRecord_Configuration() { this.Map(m =&gt; { m.MapInheritedProperties(); m.ToTable("ReceivedRecords"); }); } } </code></pre> <p>And the following for SentRecord:</p> <pre><code>public class SentRecord_Configuration : EntityTypeConfiguration&lt;SentRecord&gt;{ public SentRecord_Configuration() { this.Map(m =&gt; { m.MapInheritedProperties(); //In order to map the properties declared in RecordBase m.ToTable("SentRecords"); }); } } </code></pre> <p>But once I run this, I get the following error when EF is trying to initialize my database:</p> <pre><code>Problem in mapping fragments starting at lines 455, 1284: An entity from one EntitySet is mapped to a row that is also mapped to an entity from another EntitySet with possibly different key. Ensure these two mapping fragments do not map two unrelated EntitySets to two overlapping groups of rows. </code></pre> <p>I'm not sure what to do in order to set this up in the TPC way I described above? Or should I stick with TPH which works?</p> <p>Thanks in advance!</p>
 

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