locked
How to create a Model in vb where Table Column names contain spaces? RRS feed

  • Question

  • User2142845853 posted

    Have a VB.net webforms app, am setting up the logic for the Gridview.  But in the SQL table all columns have spaces so

    Public Class ASTR9
        Public Property Id As Integer             ok
        Public Property Plant Category as String  fail
        Public Property Plant Color as String     fail
        or
        Public Property [Plant Color] as String    fail

    In other tables, the names would always have no space at all.  It would be Plant_Category, etc.  https://www.youtube.com/watch?v=ZUCsHxBEPeg&t=68s  at 1:53 as a good example of whats being done here.  Does it matter that the name in the class isnt a literal match to whats in the SQL table's column name?

    Monday, January 13, 2020 7:29 PM

Answers

  • User281315223 posted

    You can use a ColumnAttribute to decorate your properties with names that corresponding to your database:

    Public Class ASTR9
        <Column("Id")>
        Public Property Id As Integer            
        <Column("Plant Category")>
        Public Property PlantCategory as String
        <Column("Plant Color")>
        Public Property PlantColor as String   

    Properties cannot have spaces, so this attribute should help with binding the values appropriately if your populating query has spaces. Otherwise, you could just alias or rename your columns entirely (without spaces) to avoid the issue completely.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 13, 2020 8:31 PM
  • User475983607 posted

    Class properties names cannot contains spaces.  See the VB.NET programming guide for property naming conventions.

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/program-structure/naming-conventions

    If you are using EF and generating the classes then the code generator will use an underscore to represent space.  There's also the column and table attributes which allow you to map a table column to a property.

    <Table("Enrollment")>
    Partial Public Class Enrollment
        <Column("Enrollment ID")>
        Public Property EnrollmentID As Integer
    End Class

    The EF 6 docs cover the details.

    https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/data-annotations

    rogersbr

    Does it matter that the name in the class isnt a literal match to whats in the SQL table's column name?

    Ideally, column names match the property names but it is not required.  Keep in mind, you can also alias the column names to match the property name.

    SELECT [Column With a Space] AS ColumnWithASpace
    FROM TheTable

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 13, 2020 8:35 PM

All replies

  • User281315223 posted

    You can use a ColumnAttribute to decorate your properties with names that corresponding to your database:

    Public Class ASTR9
        <Column("Id")>
        Public Property Id As Integer            
        <Column("Plant Category")>
        Public Property PlantCategory as String
        <Column("Plant Color")>
        Public Property PlantColor as String   

    Properties cannot have spaces, so this attribute should help with binding the values appropriately if your populating query has spaces. Otherwise, you could just alias or rename your columns entirely (without spaces) to avoid the issue completely.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 13, 2020 8:31 PM
  • User475983607 posted

    Class properties names cannot contains spaces.  See the VB.NET programming guide for property naming conventions.

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/program-structure/naming-conventions

    If you are using EF and generating the classes then the code generator will use an underscore to represent space.  There's also the column and table attributes which allow you to map a table column to a property.

    <Table("Enrollment")>
    Partial Public Class Enrollment
        <Column("Enrollment ID")>
        Public Property EnrollmentID As Integer
    End Class

    The EF 6 docs cover the details.

    https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/data-annotations

    rogersbr

    Does it matter that the name in the class isnt a literal match to whats in the SQL table's column name?

    Ideally, column names match the property names but it is not required.  Keep in mind, you can also alias the column names to match the property name.

    SELECT [Column With a Space] AS ColumnWithASpace
    FROM TheTable

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 13, 2020 8:35 PM