3

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; }
    }
1
  • Can you show your DbContext? Commented Sep 17, 2013 at 9:25

1 Answer 1

5

When dealing with inheritance in the Entity Framework you should access the entities through their parent DbSet. This means that in your case, you should access your TableB entities trough the TableA property on your DbContext. The TableB property should be removed.

If you would query on TableA you would get all entities that inherit from TableA (in your case only TableB entities but there could be more). If you want to filter down to TableB entities you can use the OfType method like this:

from b in context.TableA.OfType<TableB>() 
select b;
Sign up to request clarification or add additional context in comments.

6 Comments

"The TableB property should be removed." do you mean the ID should be removed from TableB as it inherits from TableA?
Yes the Id property should be removed but also the TableB property on your DbContext
+1 that works thanks. got a question if you dont mind :).. what if I had TableC that derived from TableB with its own primary ID but TableB having a foreign key to TableC do I need to specify it on TableB?
If C derives from B it won't have it's own ID. You need to share the ID in the whole hierarchy. On a side note, I would try to lose the ID of tables and foreign keys. You are working Code First. Try to think in entities and relations. Just let EF figure out the default mapping and try to adjust it when necessary
Ok thanks for explanation, can you explain what you mean by loosing the ID of tables and foreign keys. do you mean in classes just delete foreign and primary keys and the relation would be sufficient?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.