Efcore: Is there any way to easily specify the table/column name conventions for model mappings?

Created on 6 Apr 2020  路  5Comments  路  Source: dotnet/efcore


In OnModelCreating methods we can use FluentAPI to specify the table/column names. The column name uses the same name of the model property. I am thinking could we have a method that can specify the name conventions? For example, using the same property name/using camel case name, etc.


For SQL Server, we can use the same property name as the column name. However, sometimes we hope to use some other naming conventions. More specifically, sometimes the below column name convention is preferred:

user_name

For this case, we have to manually specify each table/column to apply the new mapping name.

Can we have a method to specify which convention will be applied when we create the model mappings, like:

builder.UseTableNamingStrategy(NamingStrategy namingStrategy)
builder.UseColumn(NamingStrategy namingStrategy)

The default argument of NamingStrategy would be the current behavior, other options would be LowerCase, LowerCaseWithUnderscore, something like that.

This feature is similar to NamingStrategy API in JSON.NET. FYI: https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Serialization_NamingStrategy.htm

Thanks.

closed-question customer-reported

All 5 comments

See https://github.com/efcore/EFCore.NamingConventions
It supports lowercase/UPPERCASE/snake_case (none which is default of EF Core).

Awesome, thanks @smitpatel

Anyway, it would be better if ef.core provides a built-in feature like this. Thanks.

We discussed this and decided that while this should continue to live as an extension, it could be documented (see https://github.com/dotnet/EntityFramework.Docs/issues/2281) and the extensions repo/org cleaned up (see https://github.com/dotnet/EntityFramework.Docs/issues/2280).

Thanks @ajcvickers

Was this page helpful?
0 / 5 - 0 ratings