Skip to content

Latest commit

 

History

History
67 lines (47 loc) · 2 KB

quick-start.md

File metadata and controls

67 lines (47 loc) · 2 KB
title Quick Start
description Register an explicit map and execute a normal Dapper query.
Home/Getting Started/Quick Start

Required packages: Dapper.FluentMap, Microsoft.Data.Sqlite

Problem solved

Register an explicit map and execute a normal Dapper query.

When to use it

Use this capability when the column shape and .NET model should remain separated by explicit, reviewable configuration.

Example

using Dapper;
using Dapper.FluentMap;
using Dapper.FluentMap.Mapping;
using Microsoft.Data.Sqlite;

FluentMapper.Initialize(config =>
{
    config.AddMap<CustomerMap>();
});

FluentMapper.Validate();

using var connection = new SqliteConnection("Data Source=:memory:");
var customer = connection.QuerySingle<Customer>(
    "SELECT 7 AS customer_id, 'Ada' AS customer_name;");

public sealed class Customer
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
}

public sealed class CustomerMap : EntityMap<Customer>
{
    public CustomerMap()
    {
        Map(x => x.Id).ToColumn("customer_id");
        Map(x => x.Name).ToColumn("customer_name");
    }
}

How it works

FluentMap registers typed metadata and applies it through the matching materialization path. SQL, connections, parameters, and lifecycle remain responsibilities of the application and Dapper.

Constraints

:::caution Initialize before concurrent queries and treat the effective configuration as read-only afterward. :::

Related references