Mapping classes to tables

MicroLite uses an opt-in approach to mapping which is achieved by applying attributes to classes and properties.

A class is mapped to a table or view by applying the TableAttribute.

using MicroLite.Mapping;

[Table(schema: "Sales", name: "Customers")]
public class Customer { }

Once the table mapping is defined, we need to add the ColumnAttribute to each property to describe which column it maps to. Any property that you wish to map must have a public get and set.

public CustomerStatus Status { get; set; }

Lastly, we need to add the IdentifierAttribute to the property on the class which maps to the primary key column in the database table (note: composite keys are not supported). The IdentifierAttribute requires that we specify the IdentifierStrategy.

public int Id { get; set; }

There are a number of supported identifier strategies:

  • Assigned – This strategy means that the value of the identifier property is assigned before the instance is passed to ISession.Insert().
  • Guid – This strategy means that a new GUID value will be automatically assigned to the identifier property when the instance is passed to ISession.Insert() before the insert sql is generated.
  • Identity – This is the default strategy and means that the value of the identifier is generated by the database when a new row is inserted (e.g. SQL Identity or a newid() default value). This strategy will update the identifier value on the inserted object when the transaction completes.

This article was written for version 1.2.2 of the MicroLite framework.


3 thoughts on “Mapping classes to tables

  1. Pingback: MicroLite 4.0 Released | MicroLite ORM

    1. trev Post author

      Hi, composite keys aren’t something I’m planning to add support for, they are overly complicated and introduce the possibility of major data loss if only part of the key is supplied


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s