Case insensitive name of tables and properties in Entity Framework 7
Override DelimitIdentifier in NpgsqlSqlGenerationHelper like this:
public class SqlGenerationHelper : NpgsqlSqlGenerationHelper { public override string DelimitIdentifier(string identifier) => identifier.Contains(".") ? base.DelimitIdentifier(identifier) : identifier; }
Replace ISqlGenerationHelper with your class using ReplaceService method:
public class MyContext : DbContext { public virtual DbSet<MyTable> MyTable { get; set; } public MyContext(DbConnection connection) : base(new DbContextOptionsBuilder().UseNpgsql(connection) .ReplaceService<ISqlGenerationHelper, SqlGenerationHelper>() .Options) { } }
As you can see in NpgsqlSqlGenerationHelper.cs:
static bool RequiresQuoting(string identifier)
{
var first = identifier[0];
if (!char.IsLower(first) && first != '_')
return true;
Npgsql thinks that identifiers that start with upper-case letter needs quoting. After a bit of thinking I implemented a solution described in https://andrewlock.net/customising-asp-net-core-identity-ef-core-naming-conventions-for-postgresql/ (converts all PascalCase identifiers to snake-case). It is a bit simplistic right now but I how EF Core soon will provide a way to define custom naming conventions.
To do this, you would need to swap out the SQL generation service with your own, quote-less, lowercase version. To do this, you will need to understand how EF uses DI (try reading Understanding EF Services), and need to replace the service that generates SQL. In EF, this could be ISqlGenerationHelper
, IMigrationsSqlGenerator
, or IUpdateSqlGenerator
depending on the circumstance..