How to change the table name to lower case in generated tables?

mc 5,511 Reputation points
2021-07-29T08:08:28.177+00:00

I am using entityframework core 5.0 + asp.net core web

and it will generate table name or column name as the class name or property name.

for example I have class

public class MyClassA
{
public int Id{get;set;}
public DeviceNo{get;set;}
public CreatedTime{get;set;}

}

the table name will be MyClassA and the column name will be Id;DeviceNo;CreatedTime.

how to get the table name my_class_a and the column name id.device_no .created_time while in Add-Migration?

I know can use the Column("my_class_a") attribute but there is so many properties I do not want to specify each of them.

Developer technologies | .NET | Entity Framework Core
Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,656 Reputation points
    2021-07-30T02:05:02.897+00:00

    Hi 6666666,
    You can try to use EFCore.NamingConventions which automatically makes all your table and column names have snake_case naming.
    More details you can refer to this link.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Viorel 122.6K Reputation points
    2021-07-29T08:30:56.403+00:00

    Try something like this:

    [Table("my_class_a")]
    public class MyClassA
    {
       [Column("id")]
       public int Id{get;set;}
    
       [Column("device_no")]
       public int DeviceNo{get;set;}
    
       [Column("created_time")]
       public DateTime CreatedTime{get;set;}
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.