Skip to content

Entity Framework ColumnAttribute [Mapping]

Victor Tomaili edited this page May 3, 2021 · 1 revision

(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](TableNameAttribute [Mapping])

Clone this wiki locally