With version 4 of the Entity Framework, you have the option to doing what they call Code First. The thing is that the naming of your classes and fields/properties need to match the database from what I know. I am using MySQL and as such I use all lowercase and underscores in naming as capitalization is not supported on all OSes and can make MySQL act weird if you try to force it. The issue is that I use the standard C# naming conventions of using camelCase and PascalCase. Is there a way to map the naming in my database to my classes in C# without using the entity model designer (the .edmx file) as the design seems a bit buggy. Thanks.
1 Answer
You can use the overridable method OnModelCreating which will run on first instantiation of the class. There you can override individual tables like so
public class Context : DbContext {
//your models
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<SomeTable>()
.MapSingleType()
.ToTable("YourTableNameInDatabase");
}
}
It's that simple :)
2 Comments
ryanzec
The model classes does extend or anything so there are no override-able methods since it is just a POCO.
Buildstarted
You're correct, however the DbContext class is the class with the override.