0

Currently, if I want to use Fluent API to stipulate the table-per-type inheritance strategy, I have to do the following:

modelBuilder.Entity<User>().ToTable("User");
modelBuilder.Entity<ContentItem>().ToTable("ContentItem");
modelBuilder.Entity<MarketItem>().ToTable("MarketItem");

If I somehow forget to add a command for a new inherited entity it will break my schema.

What I would like to do, in pseudo code, is:

foreach (ModelType T in AllModelTypes)
{
    modelBuilder.Entity<T>().Table(T.ToString());
}

Is there anything built-in to EF Fluent API that accomplishes this?

If not, is there a way I can achieve the foreach loop as described above (iterating over types?)

MichaC
  • 13,104
  • 2
  • 44
  • 56
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126

1 Answers1

2

Using Entity Framework 6 this should do what you want:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);    
        modelBuilder.Types().Configure(t =>t.ToTable(t.ClrType.Name));
    }

Update

Here's a link to a msdn article on Custom Code First Conventions

Olav Nybø
  • 11,454
  • 8
  • 42
  • 34