Mapping MySQL database tables/fields to C# entity classes manually
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 no开发者_高级运维t 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.
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 :)
精彩评论