I'm using the Entity Framework Core, and I have a template that uses inheritance. I use the TPH (Table Per Hierarchy) standard that maps me all classes of the same super class in the same table and uses a discriminator column to distinguish them.
When I write a data in the database, all data goes correctly. But when I try to retrieve this data, I can not capture the objects related to the foreign key.
public class Computador : ProdutoBase
{
public int CpuId { get; set; }
[Required]
public Processador Cpu { get; set; }
[Required]
public MemoriaRam Memoria { get; set; }
public int HdId { get; set; }
[Required]
public HD Hd { get; set; }
public int PlacaMaeId { get; set; }
public PlacaMae PlacaMae { get; set; }
[Required]
public decimal Preco { get; set; }
public int CodigoRef { get; set; }
protected Computador() : base()
{
}
public Computador(string marca, string modelo, decimal preco) : base()
{
Marca = marca;
Modelo = modelo;
Preco = preco;
}
public Computador(string marca, string modelo, decimal preco, Processador cpu, MemoriaRam ram, HD hd, PlacaMae placaMae) : base()
{
Marca = marca;
Modelo = modelo;
Preco = preco;
Cpu = cpu;
Memoria = ram;
Hd = hd;
PlacaMae = placaMae;
}
public void AddComponentes(Processador cpu, MemoriaRam ram, HD hd, PlacaMae placaMae)
{
Cpu = cpu;
Memoria = ram;
Hd = hd;
PlacaMae = placaMae;
}
}
Database
Json
{"cpuId":1,"cpu":null,"memoria":null,"hdId":2,"hd":null,"placaMaeId":4,"placaMae":null,"preco":5000.00,"codigoRef":0,"id":5,"marca":"Dell","modelo":"Inspiron","urlImagem":"http://www.marcheartdevie.com.br/shop/img/p/br-default-home_default.jpg"}