# 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**](https://docs.flexbase.in/data-and-providers/relational-db/entity-framework-core) | Full-featured ORM with change tracking, LINQ queries, and migrations | Complex domain models, navigation properties, automatic change tracking |
| [**Dapper (Flex MicroORM)**](https://docs.flexbase.in/data-and-providers/relational-db/dapper)               | 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](https://docs.flexbase.in/data-and-providers/relational-db/entity-framework-core)
   * [Dapper Setup](https://docs.flexbase.in/data-and-providers/relational-db/dapper)
3. Configure your database provider
4. Start building!

## See Also

* [Full-Text Search](https://docs.flexbase.in/data-and-providers/data-stores/search-store-full-text) - 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
