有没有办法让EF数据库首次迁移遵循C#命名约定?

Jac*_*rsi 2 c# asp.net-mvc entity-framework-6

我正在使用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`)
);
Run Code Online (Sandbox Code Playgroud)

这导致生成以下类:

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; }
}
Run Code Online (Sandbox Code Playgroud)

请注意,类名称和属性与表模式的名称和属性直接匹配.有没有办法让这个类按照下面的每个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; }
}
Run Code Online (Sandbox Code Playgroud)

jsc*_*eit 5

你是如何进行数据库首次迁移的?

如果从架构生成数据库并运行此命令(在visual studio包管理器控制台中),我发现生成的代码遵循C#标准命名约定.

$tables = @('table_name')
Scaffold-DbContext $connstring Microsoft.EntityFrameworkCore.SqlServer -OutputDir 'DataAccess/Models' -Tables $tables -Force
Run Code Online (Sandbox Code Playgroud)

您需要Microsoft.EntityFrameworkCore.Toolsnuget包才能访问Scaffold-DbContextcmdlet.

资料来源:https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

  • 如果你不介意一点hackery,你可以创建一个新的库(在当前sln的上下文之外),具有匹配4.6.x的命名空间,生成你的上下文,并移动文件 (2认同)