I am trying to store a table using the table-per-hierarchy pattern but instead of columns for each derived field, I want to store it as json. I am simply doing the example from the inheritance section in the .net entity framework core documentation as such:
class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<RssBlog>().HasBaseType<Blog>();
}
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}
public class RssBlog : Blog
{
public string RssUrl { get; set; }
}
This will work fine and store every property as a column as seen in the screenshot here:
What i am trying to achieve is storing the properties as a json column instead of multiple columns. It would probably look something like this:
| BlogId | Discriminator | json |
|--------|---------------|-----------------------------------------------------------------------------------------------|
| 1 | Blog | {"Url": "http://blogs.msdn.com/dotnet"} |
| 2 | RssBlog | {"Url": "http://blogs.msdn.com/adonet", "RssUrl": "http://blogs.msdn.com/b/adonet/atom.aspx"} |
The main idea is that I want to store inherited objects of a type to the same table, exactly as the TPH pattern allows, but using a json column instead of multiple columns.
Is this possible to achieve in EF Core 2.2?