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

TPT: Relationships to derived type have FK to Key column on base table #21975

Closed
eraffel-MDSol opened this issue Aug 7, 2020 · 0 comments · Fixed by #22039
Closed

TPT: Relationships to derived type have FK to Key column on base table #21975

eraffel-MDSol opened this issue Aug 7, 2020 · 0 comments · Fixed by #22039
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@eraffel-MDSol
Copy link

If I have an class in a one-to-one relationship with a TPT derived class, when I generate the migrations, the FK is to the Key column on the base class, rather than the derived class. I understand this will work (especially along with #21943), and that it makes sense because that property belongs to the base CLR class, but I would think it would be more ideal to have the FK to the Key column on the derived table.

Steps to reproduce

Generate a migration for the following context and models

    public class MyContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Parent>().ToTable("Parent");
            modelBuilder.Entity<ParentBase>().ToTable("ParentBase");
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseSqlServer("Server=localhost;Database=Test;Trusted_Connection=True;");
        }
    }

    public abstract class ParentBase
    {
        [Key]
        public Guid Id { get; set; }

        public string BaseName { get; set; }
    }

    public class Parent : ParentBase
    {
        public string Name { get; set; }

        public ParentThing Thing { get; set; }
    }

    public class ParentThing
    {
        public Guid Id { get; set; }

        public Guid ParentId { get; set; }

        public Parent Parent { get; set; }

        public string ThingName { get; set; }
    }

Further technical details

EF Core version: 5.0.0-rc.1.20406.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.1
Operating system: Windows 10
IDE: Visual Studio 2019 16.6.5

@AndriySvyryd AndriySvyryd self-assigned this Aug 7, 2020
@ajcvickers ajcvickers added this to the 5.0.0 milestone Aug 10, 2020
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 12, 2020
@AndriySvyryd AndriySvyryd removed their assignment Aug 12, 2020
@ghost ghost closed this as completed in #22039 Aug 13, 2020
ghost pushed a commit that referenced this issue Aug 13, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-rc1 Aug 14, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-rc1, 5.0.0 Nov 7, 2020
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants