# Relational Db

FlexBase provides flexible options for working with relational databases, supporting both full-featured ORM (Entity Framework Core) and high-performance micro-ORM (Dapper/Flex MicroORM) approaches.

## Available Options

| Option                                                                                  | Description                                                          | Best For                                                                |
| --------------------------------------------------------------------------------------- | -------------------------------------------------------------------- | ----------------------------------------------------------------------- |
| [**Entity Framework Core**](/data-and-providers/relational-db/entity-framework-core.md) | Full-featured ORM with change tracking, LINQ queries, and migrations | Complex domain models, navigation properties, automatic change tracking |
| [**Dapper (Flex MicroORM)**](/data-and-providers/relational-db/dapper.md)               | High-performance micro-ORM with provider-agnostic abstractions       | High-performance reads, bulk operations, complex SQL queries            |

## Quick Comparison

| Feature                   | EF Core              | Dapper           |
| ------------------------- | -------------------- | ---------------- |
| **Performance**           | Good                 | Excellent        |
| **Abstraction Level**     | High                 | Low              |
| **Change Tracking**       | ✅ Automatic          | ❌ Manual         |
| **LINQ Support**          | ✅ Full               | ❌ Raw SQL        |
| **Migrations**            | ✅ Built-in           | ❌ Manual         |
| **Navigation Properties** | ✅ Lazy/Eager loading | ❌ Manual mapping |
| **Learning Curve**        | Moderate             | Minimal          |
| **Bulk Operations**       | Good                 | Excellent        |

## Supported Database Providers

Both EF Core and Dapper support multiple database providers:

* **SQL Server** (default)
* **PostgreSQL**
* **MySQL / MariaDB**
* **SQLite**
* **Oracle**

## When to Use Each

### Use Entity Framework Core when:

* You have complex domain models with relationships
* You need automatic change tracking
* You want to use LINQ for type-safe queries
* You need automatic database migrations
* You prefer higher-level abstractions

### Use Dapper when:

* Performance is critical
* You're comfortable with SQL
* You need to execute complex, optimized queries
* You're doing bulk operations
* You want minimal overhead

## Can I Use Both?

**Yes!** Many applications use both:

* **EF Core** for complex domain models and transactional operations
* **Dapper** for high-performance read queries and reporting

```csharp
// Register both in DI
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));

builder.Services.AddFlexDapper(options =>
{
    options.ConnectionString = connectionString;
    options.ProviderName = "SqlServer";
});

// Use EF Core for writes
public class OrderService
{
    private readonly ApplicationDbContext _context;
    private readonly IFlexMicroOrmRepository _dapper;

    // Write with EF Core (change tracking, transactions)
    public async Task CreateOrderAsync(Order order)
    {
        _context.Orders.Add(order);
        await _context.SaveChangesAsync();
    }

    // Read with Dapper (high performance)
    public async Task<IReadOnlyList<OrderSummary>> GetOrderSummariesAsync()
    {
        return await _dapper.QueryAsync<OrderSummary>(
            "SELECT * FROM OrderSummariesView");
    }
}
```

## FlexBase Integration

Both approaches integrate seamlessly with FlexBase features:

* **Provider-agnostic SQL** - Use `ProviderSqlMap` to write database-specific queries
* **Async query extensions** - Use `*AsyncFlex` methods that work across both
* **Soft delete filtering** - Automatic filtering of soft-deleted records
* **Audit trail** - Automatic audit logging of changes
* **Multi-database support** - Switch providers without code changes

## Getting Started

1. Choose your approach (or use both)
2. Follow the specific guide:
   * [Entity Framework Core Setup](/data-and-providers/relational-db/entity-framework-core.md)
   * [Dapper Setup](/data-and-providers/relational-db/dapper.md)
3. Configure your database provider
4. Start building!

## See Also

* [Full-Text Search](/data-and-providers/data-stores/search-store-full-text.md) - Search capabilities across databases
* [Provider SQL Map](https://github.com/sumeru-flexbase/flexbase-docs/blob/main/ReferenceDocs/README_ProviderSqlMap.md) - Multi-database SQL support
* [Audit Trail](https://github.com/sumeru-flexbase/flexbase-docs/blob/main/ReferenceDocs/README_AuditTrailInterceptor.md) - Automatic audit logging


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.flexbase.in/data-and-providers/relational-db.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
