Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inheritance using a navigation Property #14601

Closed
alessandroros opened this issue Feb 4, 2019 · 2 comments
Closed

Inheritance using a navigation Property #14601

alessandroros opened this issue Feb 4, 2019 · 2 comments

Comments

@alessandroros
Copy link

alessandroros commented Feb 4, 2019

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

@ajcvickers
Copy link
Member

@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.

@alessandroros
Copy link
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.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants