EF doesn't Create my table

Go To StackoverFlow.com


I have three class, using TPH(table per hiracy):

abstract class Cat { int Id;    string Name; }
class ItemCat: Cat { }
class PhotoCat: Cat{ }


class Db:DbContext
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<Db,Migrations.Configuration>());
      DbSet<ItemCat> ItemCats;
      DbSet<PhotoCat> PhotoCats;

and also have Seed() method to seed ItemCats

update-database run well and seed table.

But EF doesn't create my ItemCat! and insteed it create Cats! & seed it. i don't have cats in DbContext.

I also add [Table("ItemCats")] to ItemCat class, ItemCats table appear but just have id column in it!!

I think it's because ItemCat & PhotoCat classes are empty & just inherit from Cat class. it's strange for me.

2014-03-30 21:09
by Mohammadreza
Have you tested your theory that adding a field to either the ItemCat or PhotoCat classes will result in seperate tables - Oren Hizkiya 2014-03-30 21:25
yes. doesn't work. is there any window in IDE to view order of execution of a recercive method - Mohammadreza 2014-03-30 21:39
Do you mean recursive? Also, can you post your configuration - Oren Hizkiya 2014-03-30 21:44
Can you also clarify what your end goal is? What tables would you like to end up with? One for ItemCat and one for PhotoCat - Oren Hizkiya 2014-03-30 21:46
actually my app works well, but it was strange for me that EF Create & seed my ItemCats seed in Cats. yes recursive, found it, stack cal - Mohammadreza 2014-03-30 21:56
Is the recursive call you are discussing unrelated to this question? It is not that strange that enabling Table per Heirarchy would result in what you have. Table per Heirarchy means that Entity Framework will generate one table per derived classes. See link. Sounds like what you want it TPT - Table Per Type - Oren Hizkiya 2014-03-30 22:02
Recursive wasn't belong to this post. right is with you dear @Oren actually i want TPT, i think make mistake. sorry i'm new to code firs - Mohammadreza 2014-03-30 22:13
No worries, we've all been there. Good luck - Oren Hizkiya 2014-03-30 22:14


Changing your configuration to Table Per Type should give the tables you expect. More details of how table per type generates tables are located here.

2014-03-30 22:09
by Oren Hizkiya