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.
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>();
}
}
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.
Member
1 Points
4 Posts
Confused by Inheritance and EF Core
Jul 02, 2019 12:20 AM|Rob Sharratt|LINK
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
Contributor
2253 Points
735 Posts
Re: Confused by Inheritance and EF Core
Jul 02, 2019 08:53 AM|Xing Zou|LINK
Hi Rob,
I could not reproduce your problem, it both works in Postgres DB and SQL Server.Below is my model:
DbContext:
Startup.cs:
Refer to
http://www.npgsql.org/efcore/
https://docs.microsoft.com/en-us/ef/core/modeling/inheritance
Best Regards,
Xing
Member
1 Points
4 Posts
Re: Confused by Inheritance and EF Core
Jul 02, 2019 09:05 AM|Rob Sharratt|LINK
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