Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming C# classes from bad table columns

I have used Entity Framework and a SQL Server database until now. So I can represent my table name as a class name and property names as properties like following.

class Product{ 
      public string Id { get; set;} 
      public string Name { get; set;} 
}

The table and column names are the same with my class.

But now I will work a project that uses a Postgresql database. Table names and column names are like this.

  • Tables products, product_categories (lowercase)
  • Columns product_id, product_name, category_id, ....

So I do not want to use class names like this:

class products { 
      public string product_id { get; set; }
      public string product_name { get; set; }
      public string category_id { get; set; }
}

This looks like an ugly naming conventions. How can I solve this issue?

like image 621
barteloma Avatar asked Dec 16 '14 19:12

barteloma


People also ask

What is the naming convention for C?

Naming Conventions rules for Variables and Methods (Functions) are: It should begin with an alphabet. There may be more than one alphabet, but without any spaces between them. Digits may be used but only after alphabet.

Does C use CamelCase or underscore?

Camel case for class names or lowercase+underscores (camel case is more common in my experience). Structs are used rarely (and typically because a library requires them, otherwise you'd use classes).

Should I use CamelCase C?

One popular convention is that leading capital letter CamelCase is used for the names of structs and classes, while normal camelCase is used for the names of functions and variables (although sometimes variables are written in c-style to make the visual separation between functions and variables more clear).


1 Answers

Use table and column attributes. From MSDN example:

[Table("InternalBlogs")]
public class Blog
{   
    [Column("BlogDescription", TypeName="ntext")]
    public String Description {get;set;}
}
like image 78
mason Avatar answered Oct 20 '22 01:10

mason