Skip to content

Object Mapping

Ophir Oren edited this page Oct 22, 2016 · 2 revisions

Poco.Sql works out-of-the-box on any .NET class and without the need to configure or map anything. However, there might be cases where we would like to apply mapping on objects - map relationships between objects, map object properties to database fields, ignore properties, set custom table name for object, define stored procedures, and more...

To allow such mapping, Poco.Sql has a mapping object that helps you define mappings between your objects and the database.

In order to make things easy to configure and familiar, the entity mapping was designed with the same conventions as Entity Framework - so for those who decide to move from Poco.Sql to EntityFramework later on (or the other way around), the transition will be less painful and more familiar.

Creating a mapping object

To create a mapping object, create a new class that inherits from Poco.Sql.PocoSqlMapping<T>

public class UserMap : PocoSqlMapping<User>
{
    public UserMap()
    {
        // mapping configuration
    }
}

Mapping Options

Table Name and Schema

Table Name

By default, Poco.Sql will use your object class name as the table name (you can set the PluralizeTableNames option when configuring Poco.Sql. See 'Configuration' wiki for more information).

To set the table name use the ToTable method:

this.ToTable("Users");
Database Schema

By default, no schema name will be used as part of your generated sql statements. However, you can set the schema name using 'SchemeName' method:

this.SchemeName("Sales");

Primary Key

You can define the primary key of the table by calling the HasKey method and select the key field from your object.

this.HasKey(t => t.UserId);

You can tell Poco.Sql that the primary key is auto-generated in the database by calling the AutoGenerated method. When creating insert queries, Poco.Sql will take this into consideration.

this.HasKey(t => t.UserId).AutoGenerated();

Mapping Object Properties

Poco.Sql will use reflection to find all the public properties of your object and use them as field names when creating SQL statements. Not always, you would like to use all fields, or the name of the property might be different from the name of the field in the database.

You can change properties behavior by using the Property method followed by the options you'd like to set:

this.Property(t => t.Name)

The Property method returns a special object - PropertyMap which allows configuration of the different options of the object property.

The PropertyMap object allows the following configuration per property:

Name Method Description
HasColumnName HasColumnName(string columnName) Map the property to a specific column in the database. For example: Birthday property will map to column name USER_BIRTHDAY.
HasMaxLength HasMaxLength(int maxLength) Set the max length of the field as it is defined in the database.
Ignore Ignore() Ignore this property. It will not be included in any of the queries.
IsRequired IsRequired() : IsRequired(bool required) Set the property as required property when inserting data. The value of the property cannot be null.
Clone this wiki locally