ColumnAttribute [Mapping]

Estrusco edited this page Apr 22, 2016 · 2 revisions
Clone this wiki locally

(from the official guide)

[namespace: Serenity.Data.Mapping] - [assembly: Serenity.Data]

By default a property is considered to match a column in database with the same name.

You can map a property to some other column name in database using Column attribute:

public class CustomerRow : Row
{
   [Column("street_address")]
   public string StreetAddress
   {
      get { return Fields.StreetAddress[this]; }
      set { Fields.StreetAddress[this] = value; }
   }
}

Now the query becomes:

SELECT
T0.street_address AS [StreetAddress]
FROM Customer T0

It is also possible to manually add brackets:

public class CustomerRow : Row
{
   [Column("[street_address]")]
   public string StreetAddress
   {
      get { return Fields.StreetAddress[this]; }
      set { Fields.StreetAddress[this] = value; }
   }
}
SELECT
T0.[street_address] AS [StreetAddress]
FROM Customer T0

If SqlSettings.AutoQuotedIdentifiers is true, brackets are automatically added. Use SqlServer specific brackets ( [] ) if you need to work with multiple database types. These brackets are converted to dialect specific quotes (double quote, backtick etc.) before running queries. But, if you only target one type of database, you may prefer using quotes specific to that database type.

See also: TableNameAttribute