2

I'm developing a web application using ASP.Net MVC 5, and Entity Framework 6. I've performed a database first migration based off of the below database schema.

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

This results in the following class being generated:

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; }
}

Note that the class name, and properties directly match that of the table schema. Is there a way to have this class follow Pascal Case per C# naming conventions as per below?

public partial class TableName
{
    public int Id { get; set; }
    public int SomeValue { get; set; }
    public int SomeOtherValue { get; set; }
    public int SomeOtherOtherValue { get; set; }
}

1 Answer 1

5

How did you do the database first migration?

If you generate a database from your schema and run this command (in the visual studio package manager console), I've found that the code generated follows C# standard naming conventions.

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

You'll need the Microsoft.EntityFrameworkCore.Tools nuget package to access the Scaffold-DbContext cmdlet.

Source: https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

5
  • It seems the Microsoft.EntityFrameworkCore.Tools nuget package does not support .net framework 4.5 - any ideas?
    – user5986440
    Feb 15, 2018 at 20:26
  • I've followed this guide to perform the migration.
    – user5986440
    Feb 15, 2018 at 20:32
  • Is this project greenfield? And if so is .net 4.6.x out of the question? Feb 15, 2018 at 20:33
  • Unfortunately not.
    – user5986440
    Feb 15, 2018 at 20:35
  • 2
    If you didn't mind a little hackery, you could create a new library (outside of the context of your current sln) with matching namespaces that targets 4.6.x, generate your context, and move the files over Feb 15, 2018 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy