Skip to content

Inheritance using a navigation Property #14601

Closed as duplicate
Closed as duplicate

Description

@alessandroros

I've some issues defining these relationships between my entities. A CarTemplate is base class with common properties. CoupeTemplate and SedanTemplate are 2 template of coupe and sedan cars with different attributes. A CarTemplate can have more Car

public class CarTemplate {
    public long Id { get; set; }
    public string CarType { get; set; }   //Discriminator required from TPH
    public List<Car> Cars {get;set;}
}
public class CoupeTemplate: CarTemplate {
    public string CoupeAttribute1 { get; set; }
    public string CoupeAttribute2 { get; set; }
}
public class SedanTemplate: CarTemplate {
    public string AttributeSedan1 { get; set; }
    public string AttributeSedan2 { get; set; }
}

public class Car {
    public long Id { get; set; }
    public long Attribute1Car {get;set;}
}
public class Coupe: Car{
    public string Attribute1Coupe { get; set; }
    public string Attribute2Coupe { get; set; }
}
public class Sedan: Car {
    public string Attribute1Sedan { get; set; }
    public string Attribute2Sedan { get; set; }
}

Here the modeling for the TPH on CarTemplate,

builder.Entity<CarTemplate >(
                entity =>
                {
                    // primary key
                    entity.HasKey(p => p.Id);                    

                    entity.HasDiscriminator<string>("CarType")
                        .HasValue<CoupeTemplate>("Coupe")
                        .HasValue<SedanTemplate>("Sedan")

                    entity.HasMany(a => a.Cars ).WithOne(e => e.CarTemplate).HasForeignKey(a => a.CarTemplateId);
                });

Now I would like to use the same CarType attribute on CarTemplate to determine the type of a "Car". So what I would do with Fluent API is something like

builder.Entity<Car>(
                entity =>
                {
                    // primary key
                    entity.HasKey(p => p.Id);                    

                    entity.HasDiscriminator(x=>x>CarTemplate.CarType)
                        .HasValue<Coupe>("Coupe")
                        .HasValue<Sedan>("Sedan")

                    entity.HasMany(a => a.Cars ).WithOne(e => e.CarTemplate).HasForeignKey(a => a.CarTemplateId);
                });

I would like to reuse the Discriminator of the other related entity instead of creating a new one which introduce data redundancy(e.g. Coupe value would be present in the CarTemplate table and also in the Car table)
Do you think it is possible to do something like this with EF Core? Any suggestion on the modelling is really appreciated!

Further technical details

EF Core version: 2.2
Database Provider: SQL Server
Operating system:
IDE: Visual Studio 2017 15.9.6

Activity

  1. ajcvickers commented on Feb 4, 2019

    @ajcvickers
    Contributor

    @elciopa There are some inconsistencies/missing pieces in the code above, but I think you mean that if each row in the Cars table has a FK to a row in the CarTemplates table, then that could be used to determine the type of Car in the Cars table. If so, this isn't currently supported, but could possibly be covered by #10140.

  2. alessandroros commented on Feb 5, 2019

    @alessandroros
    Author

    @elciopa There are some inconsistencies/missing pieces in the code above, but I think you mean that if each row in the Cars table has a FK to a row in the CarTemplates table, then that could be used to determine the type of Car in the Cars table. If so, this isn't currently supported, but could possibly be covered by #10140.

    Yes that's what I meant. Thanks a lot for the tip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions