I am new to EF and as per this tutorial I have set up my code-first classes to have base calls TableA's ID as primary and foreign key for TableB as below:
[Table("TableA")]
public abstract class TableA
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string Description { get; set; }
}
[Table("TableB")]
public class TableB: TableA
{
[Key]
public int ID { get; set; }
public int Width { get; set; }
}
In controller when doing a query:
var a = db.TableB.ToList();
I get 0 for tableB IDs. But while debugging I can see the base class having the appropriate IDs. so I did a loop and inside it assigned IDs using item.ID = ((TableA)(item)).ID;.
The above works as in it assigns the IDs so I can see them in my view, but is this the right way to do it? Is there any other solutions?
EDIT : Here is the dbcontext:
public class Context : DbContext
{
public ProductContext()
: base("DefaultConnection")
{
}
public DbSet<TableA> TableA { get; set; }
public DbSet<TableB> TableB { get; set; }
}