Lostaunaum Lostaunaum - 1 year ago 66
SQL Question

Mapping to columns with symbols in name

I have a database for records with column names that look like this

TexasBexarCountyMisdemeanorPublicRecordId [int] IDENTITY(1,1) NOT NULL,


FULL-NAME nvarchar NULL,

I am trying to create a C# object which is mapped to this table however I cant use the "-" symbol because the code doesnt like it. Is there a solution to this? The reason why the table has these columns is because it has been imported from a large government database. Should I create another table without these "-" in the headers? And then map the columns during the import stage?

My goal is to create a C# object that can map to the column names with the "-" symbol in it.

public class TexasBexarCountyMisdemeanorPublicRecord : Entity
public int TexasBexarCountyMisdemeanorPublicRecordId { get; set; }

public string CASE-CAUSE-NBR { get; set; }

public string FULL-NAME { get; set; }

In the C# code above the attributes are not working because of the "-" symbol.

Answer Source

In this case, you can use the Column Name attribute to override using the property name to be identical to the column name in the database.

In your case, this would be:

public string CaseCauseNumber { get; set; }

Note that this is also possible with the Fluent API:

    .Property(t => t.CaseCauseNumber )