-
Notifications
You must be signed in to change notification settings - Fork 302
Mapping
By default no mapping is required. It will be assumed that the table name will be the class name and the primary key will be 'Id' if its not specified with the attributes below.
Basic mapping is done via attributes. The most used ones are:
-
[TableName]
This takes a "name" parameter which indicates the table for which the Poco class will be mapped to. -
[PrimaryKey]
This has a "columnName" parameter which takes the column name of the primary key for the table. Multiple primary keys can be specified by separating the columns with a comma. There is also an "autoIncrement" property which is used to indicate whether the primary key column is auto incremented e.g. identity column in Sql Server. By default this is true. For Oracle there is also a "sequenceName" property which will be used when inserting data with Oracle. -
[Column]
This is used if the column name does not match the property name. -
[Ignore]
This property will be ignored and cannot be mapped to. -
[ResultColumn]
Properties marked with the Result column can be mapped into, however these properties will not be included in inserts or updates. Note: These columns will need to be explicitly specified in the SQL. It will not be included in the auto generated SQL. -
[ComputedColumn]
Properties with the Computed column attribute work the same way as the Result column attributes however they will be auto selected in the SQL. -
[SerializedColumn]
(v3+ only) Properties with the Serialized column attribute will serialize their data with the default serializer unless changed by implementing aIColumnSerializer
. There is a NPoco.JsonNet library which allows you to use theJsonNetColumnSerializer
. These are configured once by:
DatabaseFactory.ColumnSerializer = new JsonNetColumnSerializer();
[TableName("Users")]
[PrimaryKey("UserId")]
public class User
{
public int UserId { get;set; }
[Column("emailAddress")]
public string Email { get;set; }
[ResultColumn]
public string ExtraInfo { get;set; }
[Ignore]
public int Temp { get;set; }
}
If you don't like attribute based mapping, the take a look at fluent / convention based mapping