Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity framework 'table-per-concrete-class' maps to wrong table
    primarykey
    data
    text
    <p>I'm trying to do a table-per-concrete-class mapping with the Entity Framework in an ASP.NET MVC application. It's for a school assignement and i have to map to an existing (mysql) database.</p> <pre><code>public class Deelnemer { public Deelnemer() { OpgelosteDoosVragen = new List&lt;OpgelostDoosVraag&gt;(); } public string Emailadres { get; set; } public virtual IList&lt;OpgelostDoosVraag&gt; OpgelosteDoosVragen { get; set; } } } </code></pre> <p>This class contains a list of 'OpgelosteDoosVragen'. The class 'OpgelosteDoosVragen' (empty at the moment) inherits from 'AbstractOpgelost'.</p> <pre><code>public abstract class AbstractOpgelost { protected AbstractOpgelost() { OpgelostId = new Guid(); } public Guid OpgelostId { get; set; } public virtual AbstractVraag AbstractVraag { get; set; } public virtual AbstractAntwoord AbstractAntwoord { get; set; } public virtual Element Element { get; set; } } public class OpgelostDoosVraag : AbstractOpgelost { } </code></pre> <p>The mapping: </p> <pre><code>public class AbstractOpgelostMap : EntityTypeConfiguration&lt;AbstractOpgelost&gt; { public AbstractOpgelostMap() { this.HasKey(t =&gt; t.OpgelostId); this.Property(t =&gt; t.OpgelostId) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); this.HasRequired(t =&gt; t.Element).WithMany().Map(t =&gt; t.MapKey("elementNaam")); this.HasRequired(t =&gt; t.AbstractVraag).WithMany().Map(t =&gt; t.MapKey("vraagId")); this.HasRequired(t =&gt; t.AbstractAntwoord).WithMany().Map(t =&gt; t.MapKey("antwoordId")); } } public class OpgelostDoosVraagMap : EntityTypeConfiguration&lt;OpgelostDoosVraag&gt; { public OpgelostDoosVraagMap() { this.Map(t =&gt; { t.MapInheritedProperties(); t.ToTable("doos_opgelostevragen"); }); } } </code></pre> <p>OnModelCreating method:</p> <pre><code> protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new OpgelostDoosVraagMap()); modelBuilder.Configurations.Add(new DeelnemerMap()); modelBuilder.Configurations.Add(new doosMap()); modelBuilder.Configurations.Add(new doosVraagMap()); modelBuilder.Configurations.Add(new DoosAntwoordMap()); modelBuilder.Configurations.Add(new ElementMap()); modelBuilder.Configurations.Add(new AbstractAntwoordMap()); modelBuilder.Configurations.Add(new AbstractVraagMap()); modelBuilder.Configurations.Add(new AbstractOpgelostMap()); } </code></pre> <p>When I try to access the list in Deelnemer an EntityCommandExecution is thrown is thrown and it says: <code>MySql.Data.MySqlClient.MySqlException: Table 'kindermishandeling.abstractopgelosts' doesn't exist</code> The problem is entity framework wants to map to the table of the abstract class 'AbstractOpgelost' but it has to map to doos_opgelostevragen (the table of the concrete class).</p> <p>Does anyone see a problem in my mapping. I'm looking for hours and i'm desperate...</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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