0

I have two base classes where each base class has inherited subclasses:

[Table("BaseClassOne")]
public class BaseClassOne
{
    public Guid         Id      { get; set; }
    public BaseClassTwo BaseTwo { get; set; }
}

[Table("DerivedClassOne")]
public class DerivedClassOne : BaseClassOne
{
    public Guid   Id   { get; set; }
    public string Name { get; set; }
}

[Table("BaseClassTwo")]
public class BaseClassTwo
{
    public Guid         Id      { get; set; }
    public BaseClassOne BaseOne { get; set; }
}

[Table("DerivedClassTwo")]
public class DerivedClassTwo : BaseClassTwo
{
    public Guid Id { get; set; }
}

Now I would like to configure the relationships via model-builder, but without needing a discriminator:

builder
    .Entity<BaseClassOne>()
    .HasKey(b1 => b1.Id);

builder
    .Entity<DerivedClassOne>()
    .HasBaseType<BaseClassOne>();

builder
    .Entity<BaseClassTwo>()
    .HasKey(b2 => b2.Id);

builder
    .Entity<DerivedClassTwo>()
    .HasBaseType<BaseClassTwo>();

//This is where the problem starts:
builder
    .Entity<BaseClassTwo>()
    .HasOne(b2 => b2.BaseClassOne)
    .WithOne(b1 => b1.BaseClassTwo)
    .HasForeignKey<BaseClassTwo>(e => e.Id)
    .OnDelete(Rule.Cascade);

I'm trying to run a query like following I get an error that the column m.Discriminator can't be found:

_context.BaseClassesTwo
    .Select(b2 => new
    {
        val1 = b2.name,
        val2 = b2.BaseClassOne.name
    })
    .ToList();

Is this an conceptional problem or do I need to change the configuration? And when, how?

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
SNO
  • 793
  • 1
  • 10
  • 30
  • **Exactly** what version of EF Core are you using? – Dai Aug 05 '22 at 20:09
  • Also... why are you using inheritance at all here? Java-style OOP Inheritance (which C# has... unfortunately) **really** does not mesh well with Codd's relational theorem ([hence our favourite problem...](https://en.wikipedia.org/wiki/Object%E2%80%93relational_impedance_mismatch)) - also consider that EF Core (and SQL Server) still does not support _true_ `1:1` constraints (only PostgreSQL does with deferred constraints, but to my knowledge EFCore6 doesn't support this yet). – Dai Aug 05 '22 at 20:10
  • Why are you using an explicit type-argument for the `HasForeignKey` call? – Dai Aug 05 '22 at 20:19
  • I'm using inheritance to have my project extendable . But this is not the question respectively the problem. I'm using the latest oracle ef core version. In addition to that I use Fluent migrator to get the database setup. I have many points where I work with one to one relationships and inheritance successfully. But only this concept throws the mentioned exception. Maybe it's not possible? – SNO Aug 06 '22 at 13:42
  • *without needing a discriminator* -- Do you mean using TPT or TPC rather than TPH? And what is the *exact* exception message? It's always better to mention those instead of paraphrasing them. – Gert Arnold Aug 06 '22 at 15:07
  • I have TPT. The exception is an oracle exception that the column not exists – SNO Aug 06 '22 at 18:33

0 Answers0