Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Are you trying to do a TPT, where there's a separate table for Order/Tenant? If so, I think the other poster is correct, and there's a bug in EF.</p> <p>If customers/products are your based mapped classes, this might work for you.</p> <p>What happened to me when I created your database is it tried to map the abstract Order class.</p> <p>By adding:</p> <pre><code>modelBuilder.Ignore&lt;Order&gt;(); </code></pre> <p>The builder kept the mapped properties due to the MapInheritedProperties, but didn't create the table so that all the FK's were correctly created. </p> <p>I'm assuming you wanted separate tables for each of your classes like the related post above, and to not map your abstract table.</p> <p>Entire Model:</p> <pre><code> public abstract class BindableBase { } //base class for all tenant-scoped objects public abstract class TenantModelBase : BindableBase { [Key] public virtual int TenantId { get; set; } } public abstract class Order : TenantModelBase { public Customer Customer { get; set; } //works: mapped using TenantId and CustomerId public Product Product { get; set; } //again, works with TenantId and ProductId public string ProductId { get; set; } public string CustomerId { get; set; } } public class Customer : TenantModelBase { [Key] public string CustomerId { get; set; } } public class Product : TenantModelBase { [Key] public string ProductId { get; set; } } public class SpecialOrder : Order { [Key] public int SpecialOrderId { get; set; } public OtherClass OtherClass { get; set; } //this fails!, see below public string OtherClassId { get; set; } } public class SuperSpecialOrder : SpecialOrder { } public class OtherClass : TenantModelBase { public string OtherClassId { get; set; } public ICollection&lt;SpecialOrder&gt; SpecialOrders { get; set; } } public class Model : DbContext { public DbSet&lt;Customer&gt; Customers { get; set; } public DbSet&lt;Product&gt; Products { get; set; } public DbSet&lt;SpecialOrder&gt; SpecialOrders { get; set; } public DbSet&lt;SuperSpecialOrder&gt; SuperSpecialOrders { get; set; } public DbSet&lt;OtherClass&gt; OtherClasses { get; set; } protected override void OnModelCreating( DbModelBuilder modelBuilder ) { modelBuilder.Entity&lt;OtherClass&gt;() .HasKey( k =&gt; new { k.TenantId, k.OtherClassId } ); modelBuilder.Entity&lt;Customer&gt;() .HasKey( k =&gt; new { k.TenantId, k.CustomerId } ); modelBuilder.Entity&lt;Product&gt;() .HasKey( k =&gt; new { k.TenantId, k.ProductId } ); modelBuilder.Entity&lt;SpecialOrder&gt;() .Map( m =&gt; { m.MapInheritedProperties(); m.ToTable( "SpecialOrders" ); } ); modelBuilder.Entity&lt;SpecialOrder&gt;().HasKey( k =&gt; new { k.TenantId, k.SpecialOrderId } ); modelBuilder.Entity&lt;SuperSpecialOrder&gt;() .Map( m =&gt; { m.MapInheritedProperties(); m.ToTable( "SuperSpecialOrders" ); } ) .HasKey( k =&gt; new { k.TenantId, k.SpecialOrderId } ); modelBuilder.Entity&lt;SpecialOrder&gt;() .HasRequired( p =&gt; p.OtherClass ) .WithMany( o =&gt; o.SpecialOrders ) .HasForeignKey( p =&gt; new { p.TenantId, p.OtherClassId } ); modelBuilder.Entity&lt;Order&gt;() .HasRequired( o =&gt; o.Customer ) .WithMany() .HasForeignKey( k =&gt; new { k.TenantId, k.CustomerId } ); modelBuilder.Entity&lt;Order&gt;() .HasRequired( o =&gt; o.Product ) .WithMany() .HasForeignKey( k =&gt; new { k.TenantId, k.ProductId } ); modelBuilder.Ignore&lt;Order&gt;(); } } </code></pre> <p>Created Database: <img src="https://i.stack.imgur.com/uVdGf.png" alt="enter image description here"></p> <p>Hope this helps.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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