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

标签 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`)
);

这会生成以下类:

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/

相关文章:

c# - 可以订购动态类型吗?

c# - ASP.NET MVC 4 具有一个域模型用于多个应用程序和缓存

azure - 用户 '<token-identified principal>' 基于 AD token 的身份验证登录失败。在 Azure SQL 的 Entity Framework 6 中

c# - 覆盖 SaveChanges 并删除 EF 关系时为 null

c# - 使用 Binding 时出现 FontImageSource Glyph 问题

c# - 学生信息的数据绑定(bind)

asp.net-mvc - 身份 2 - 确认电子邮件,然后允许用户设置密码

asp.net - 无法加载 URL : The domain of this URL isn't included in the app's domains in Facebook Login in ASP. NET MVC

c# - 无法使用 Entity Framework 和 ASP.NET MVC 从 Models.Class 转换为 DbContext.Class

c# - 创建子项目时,Visual Studio 中的空白解决方案被覆盖