Domanda

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

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

and

class Db:DbContext
    {
    Db()
      {
        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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top