我正在使用 ASP.Net MVC 5 和 Entity Framework 6 开发一个 Web 应用程序。我已经根据以下数据库架构执行了数据库首次迁移。
CREATE TABLE IF NOT EXISTS `database`.`table_name` (
`id` INT NOT NULL AUTO_INCREMENT,
`some_value` INT NULL,
`some_other_value` INT NULL,
`some_other_ohter_value` INT NULL,
PRIMARY KEY (`id`)
);
这会生成以下类:
public partial class table_name
{
public int id { get; set; }
public int some_value { get; set; }
public int some_other_value { get; set; }
public int some_other_ohter_value { get; set; }
}
请注意,类名称和属性直接与表架构匹配。 是否有办法让此类按照 C# 命名约定遵循 Pascal Case,如下所示?
public partial class TableName
{
public int Id { get; set; }
public int SomeValue { get; set; }
public int SomeOtherValue { get; set; }
public int SomeOtherOtherValue { get; set; }
}
最佳答案
您是如何进行数据库首次迁移的?
如果您从架构生成数据库并运行此命令(在 Visual Studio 包管理器控制台中),我发现生成的代码遵循 C# 标准命名约定。
$tables = @('table_name')
Scaffold-DbContext $connstring Microsoft.EntityFrameworkCore.SqlServer -OutputDir 'DataAccess/Models' -Tables $tables -Force
您需要 Microsoft.EntityFrameworkCore.Tools
nuget 包来访问 Scaffold-DbContext
cmdlet。
来源:https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db
关于c# - 有没有办法让 EF 数据库首次迁移遵循 C# 命名约定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48815208/