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

EF Core code first Inheritance of separate tables #8728

Closed
borisdj opened this issue Jun 5, 2017 · 4 comments
Closed

EF Core code first Inheritance of separate tables #8728

borisdj opened this issue Jun 5, 2017 · 4 comments

Comments

@borisdj
Copy link

borisdj commented Jun 5, 2017

Say I have a table Company defined in following entity:

    public class Company
    {
        public Guid CompanyId { get; set; }
    
        [Required]
        public string Name { get; set; }
    
        [MaxLength(50)]
        public string Uid { get; set; }
    
        ...
    }

And I need another table CompanyHistory that will have all fields of Company extended with CompanyHistoryId, EffectiveDate, DEffectiveDate.
I have tried it like this:

    public class CompanyHistory : Company
    {
        public Guid CompanyHistoryId { get; set; }
    
        public virtual Company { get; set; }
    }

But instead of 2 tables, migration makes only 1 table 'Company' where it combines all the columns.

I could get what I want like this:

    public class CompanyHistory
    {
        public Guid CompanyHistoryId { get; set; }
    
        public Guid CompanyId { get; set; }
        public virtual Company Company { get; set; }
    
        [Required]
        public string Name { get; set; }
    
        [MaxLength(50)]
        public string Uid { get; set; }
    
        ...
    }

But can the same be achieved using inheritance, so that I don't have to write all the columns again.

Further technical details

EF Core version: 1.1.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017

@borisdj borisdj changed the title EF Core code first Inheritance of separate table EF Core code first Inheritance of separate tables Jun 5, 2017
@soeron
Copy link

soeron commented Jun 5, 2017

I am guessing you are looking for Table Per Type inheritance, which is very needed feature not yet implemented. You can look here #2266 for more info.

@borisdj
Copy link
Author

borisdj commented Jun 5, 2017

It seems so, and looks like it's not implemented yet.
However I have figured out how to achieve what I want with a little different structure.
Define first CompanyBase (abstract) class and then derive from it both Company and CompanyHistory.

    public abstract class CompanyBase
    {
        [Required]
        public string Name { get; set; }
    
        [MaxLength(50)]
        public string Uid { get; set; }
    
        ...
    }
    public class Company : CompanyBase
    {
        public Guid CompanyId { get; set; }
    }
    public class CompanyHistory : CompanyBase
    {
        public Guid CompanyHistoryId { get; set; }

        public Guid CompanyId { get; set; }
        public virtual Company Company { get; set; }

        public DateTime EffectiveDate { get; set; }

        public DateTime? DeffectiveDate { get; set; }
    }

@ajcvickers
Copy link
Member

@borisdj We typically refer to this type of mapping as TPC. This is being tracked by issue #3170, so I'm going to close this issue as a duplicate of that one.

The workaround that you posted should work as long as CompanyBase is not mapped. This implies that there cannot be any references from entity types to the CompanyBase class--they must all reference one of the derived classes.

@borisdj
Copy link
Author

borisdj commented Jun 6, 2017

Yes, and CompanyBase is not mapped, I have just added 'abstract':
abstract CompanyBase class...

@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

3 participants