Entity Framework 数据库优先方法 Pascal 案例

Entity Framework database-first approach Pascal case

我正在使用 EF Core 3.0 和数据库优先方法从 SQL 服务器数据库 Scaffold-DbContext:

dotnet ef dbcontext 
       scaffold "Server=***;Database=***;User Id=***;Password=****;" Microsoft.EntityFrameworkCore.SqlServer 
       -o Models -c "MyContext" --project=Data

生成模型时,它使用指定的变量命名,例如:

如果数据库中的列调用例如

这真的错过了我的系统......

是否有一种方法可以防止转换并生成与数据库相同的变量名,或者是否有一种超级神奇的方法可以防止这些并像 EF Core 那样使用驼峰式大小写?

而且我不是在谈论从 API 返回的对象,因为我找到了很多针对这种情况的解决方案,但从来没有找到过这个。

谢谢

在您的 .NET Core CLI 中添加 --use-database-names

dotnet ef dbcontext 
   scaffold "Server=***;Database=***;User Id=***;Password=****;" Microsoft.EntityFrameworkCore.SqlServer 
   -o Models -c "MyContext" --project=Data --use-database-names

参考

https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#dotnet-ef-dbcontext-scaffold

--use-database-names
Use table and column names exactly as they appear in the database. If this option is omitted, database names are changed to more closely conform to C# name style conventions.

在 PowerShell 中,您可以使用 UseDatabaseNames 选项(带有一个破折号)作为 a Scaffold-DbContext call 的一部分,以保留数据库模式中使用的大小写。

Scaffold-DbContext "Server=.\SERVERNAME;Database=DBNAME;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir ENTITIESDIR -Context "NameContext" -UseDatabaseNames