I am using the code first approach and I am trying to change the behavior of how EF creates my tables. I have two simple classes:
Note: I have my database dropped and recreated every time. This wouldn't happen in the real world but for testing I am trying to intercept the creation and force EF to create the database how I envision it.
public class Person
{
public int PersonId { get; private set; }
public string Forename { get; set; }
}
public class Officer : Person
{
public int OfficerId { get; private set; }
public bool IsManager { get; set; }
}
In my OnModelCreating() event I have the following:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Person>().HasKey(x => x.PersonId);
modelBuilder.Entity<Person>().ToTable("Person");
modelBuilder.Entity<Officer>().HasKey<int>(x => x.OfficerId);
modelBuilder.Entity<Officer>().ToTable("Officer");
}
Issue is when my database is created it creates the tables as:
Table: Person PersonId INT PK Forename nvarchar(50)
Table: Officer PersonId INT PK, FK IsManager BIT OfficerId INT
To me this looks wrong as it doesn't go by traditional naming conventions, i.e. TableNameId.
Any help on this is much appreciated. Thanks in advance, Onam.