Note that there are some explanatory texts on larger screens.

plurals
  1. POEF5 TPH Extra Foreign Keys being generated - How to get rid of them?
    primarykey
    data
    text
    <p>This is a sample app I've concocted which produces the same behaviour as my actual much-more-complex app. I'm obviously missing some configuration aspect somewhere, but can't figure it out for the life of me. For some reason, each collection in my KID class is receiving its' own field and foreign key in the Sweet table....which just isn't necessary as far as I can see.....? How can I stop EF generating these?</p> <p>Example classes, config and resulting migration code follows (NB, If I use TPT instead of TPH the extra fields aren't added and OwnerId is used as the relational field just fine)</p> <p>My classes:</p> <pre><code> public class Sweet { [Key] public int SweetId { get; set; } public string SweetName { get; set; } [ForeignKey("OwnerId")] public Kid Owner { get; set; } public int OwnerId { get; set; } } public class Chocolate : Sweet {} public class HardBoiled : Sweet {} public class Chewy : Sweet {} public class Kid { public int KidId { get; set; } public string FirstName { get; set; } public virtual ICollection&lt;Chocolate&gt; Chocolates { get; set; } public virtual ICollection&lt;HardBoiled&gt; BaggedSweeets { get; set; } public virtual ICollection&lt;Chewy&gt; PacketSweets { get; set; } } </code></pre> <p>My Configuration (Called from OnModelCreating)</p> <pre><code>public class SweetConfiguration : EntityTypeConfiguration&lt;Sweet&gt; { public SweetConfiguration() { Map(m =&gt; m.ToTable("Sweet")); Map&lt;Chocolate&gt;(i =&gt; i.Requires("SweetType").HasValue(1)); Map&lt;Chewy&gt;(i =&gt; i.Requires("SweetType").HasValue(2)); Map&lt;HardBoiled&gt;(f =&gt; f.Requires("SweetType").HasValue(3)); } } </code></pre> <p>The generated Migration code:</p> <pre><code> public override void Up() { CreateTable( "dbo.Kid", c =&gt; new { KidId = c.Int(nullable: false, identity: true), FirstName = c.String(), }) .PrimaryKey(t =&gt; t.KidId); CreateTable( "dbo.Sweet", c =&gt; new { SweetId = c.Int(nullable: false, identity: true), SweetName = c.String(), OwnerId = c.Int(nullable: false), Kid_KidId = c.Int(), &lt;--- DON'T NEED THIS Kid_KidId1 = c.Int(), &lt;--- OR THIS Kid_KidId2 = c.Int(), &lt;-- OR THIS! SweetType = c.Int(), }) .PrimaryKey(t =&gt; t.SweetId) .ForeignKey("dbo.Kid", t =&gt; t.Kid_KidId) &lt;!-- LIKEWISE FOR THESE THREE KEYS .ForeignKey("dbo.Kid", t =&gt; t.Kid_KidId1) .ForeignKey("dbo.Kid", t =&gt; t.Kid_KidId2) .ForeignKey("dbo.Kid", t =&gt; t.OwnerId, cascadeDelete: true) .Index(t =&gt; t.Kid_KidId) .Index(t =&gt; t.Kid_KidId1) .Index(t =&gt; t.Kid_KidId2) .Index(t =&gt; t.OwnerId); } </code></pre> <p>UPDATE:</p> <p>As it would appear my current model isn't supported I have changed my Kid class like this:</p> <pre><code>public class Kid { public int KidId { get; set; } public string FirstName { get; set; } public virtual ICollection&lt;Sweet&gt; Sweets { get; set; } [NotMapped] public ICollection&lt;HardBoiled&gt; BaggedSweets { get { return Sweets.OfType&lt;HardBoiled&gt;().ToList(); } } ... and two more read-only NotMapped properties for the other collections... } </code></pre>
    singulars
    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. This table or related slice is empty.
    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