locked
Confused by Inheritance and EF Core RRS feed

  • Question

  • User-2011450845 posted

    I'm trying to learn EF Core and I've got myself confused about how inheritance works with the code first implementation.  I've created by code first classes e.g.

    Class 1 - Card (has a number of fields including an ID)

    Class 2 - SpecialCard (inherits from Card but adds another field called SpecialCardText

    EF Core migration has created a Discriminator column but there isn't a SpecialCardText column in my Postgres DB.

    I've tried to select from the EF using SingleOrDefault and I've got back the one row I added and then tried to pull the SpecialCardText value out and it's returned it correct.  However, what I can't see is where the data actually got stored in the DB.  The Discriminator column has become "SpecialCard" whilst other entires are simply "Card".

    Can someone explain how this works and where in the DB the actual values are stored.

    Rob

    Tuesday, July 2, 2019 12:20 AM

Answers

  • User-1764593085 posted

    Hi Rob,

    EF Core migration has created a Discriminator column but there isn't a SpecialCardText column in my Postgres DB.

    I could not reproduce your problem, it both works in Postgres DB and SQL Server.Below is my model:

    public class Card
        {
            [Key]
            public int ID { get; set; }
            public string Context { get; set; }
            public string Receiver { get; set; }
        }
    
        public class SpecialCard : Card
        {
            public string SpecialCardText { get; set; }
        }

    DbContext:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUsers>
        {
            public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
                : base(options)
            {
            }
            public DbSet<Card> Cards {get;set;}
           
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
                modelBuilder.Entity<SpecialCard>().HasBaseType<Card>();
    
            }
    
        }

    Startup.cs:

     services.AddDbContext<MyDbContext>(options =>
                   options.UseNpgsql("Host=localhost;Port=5432;Database=Test;User ID=xxx;Password=xxx;"));

    Refer to 

    http://www.npgsql.org/efcore/

     https://docs.microsoft.com/en-us/ef/core/modeling/inheritance

    Best Regards,

    Xing

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 2, 2019 8:53 AM

All replies

  • User-1764593085 posted

    Hi Rob,

    EF Core migration has created a Discriminator column but there isn't a SpecialCardText column in my Postgres DB.

    I could not reproduce your problem, it both works in Postgres DB and SQL Server.Below is my model:

    public class Card
        {
            [Key]
            public int ID { get; set; }
            public string Context { get; set; }
            public string Receiver { get; set; }
        }
    
        public class SpecialCard : Card
        {
            public string SpecialCardText { get; set; }
        }

    DbContext:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUsers>
        {
            public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
                : base(options)
            {
            }
            public DbSet<Card> Cards {get;set;}
           
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
                modelBuilder.Entity<SpecialCard>().HasBaseType<Card>();
    
            }
    
        }

    Startup.cs:

     services.AddDbContext<MyDbContext>(options =>
                   options.UseNpgsql("Host=localhost;Port=5432;Database=Test;User ID=xxx;Password=xxx;"));

    Refer to 

    http://www.npgsql.org/efcore/

     https://docs.microsoft.com/en-us/ef/core/modeling/inheritance

    Best Regards,

    Xing

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 2, 2019 8:53 AM
  • User-2011450845 posted

    Thanks Xing, this was my own fault, I'd somehow missed the get; set; off the SpecialCard object, for whatever reason it still worked but I suspect it would have broken very quickly.  I'm assuming that the entity was still cached which is why it managed to have the value still and as I'm seeding the DB each time while I'm developing and tweaking it then the missing column wasn't a problem but obviously missing.

    Thanks for the code though as the simple demo of a basic working implementation immediately showed me what was wrong.  I've been going around in circles trying to find plain examples.

    Rob

    Tuesday, July 2, 2019 9:05 AM