Skip to content

shady78/MultiSchema.DbContext.Dynamic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MultiSchema.DbContext.Dynamic

A .NET solution for implementing dynamic schema switching in Entity Framework Core applications, perfect for multi-tenant architectures where schema isolation is required.

🌟 Features

  • Dynamic schema switching at runtime
  • Middleware support for schema resolution
  • Action filters for schema validation
  • Global parameter service for schema management
  • Support for header-based and query parameter-based schema selection
  • Thread-safe schema context handling

🚀 Getting Started

Prerequisites

  • .NET 8.0 or later
  • SQL Server (or compatible database)
  • Entity Framework Core 8.0 or later

Installation

  1. Clone the repository:
git clone https://github.com/shady78/MultiSchema.DbContext.Dynamic.git
  1. Add the required NuGet packages:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
  1. Configure your connection string in appsettings.json:
{
  "ConnectionStrings": {
    "DefaultConnection": "Your_Connection_String_Here"
  }
}

📋 Usage

1. Register Services

builder.Services.AddScoped<IGlobalParameterService, GlobalParameterService>();
builder.Services.AddDbContext<ApplicationDbContext>((serviceProvider, options) =>
{
    options.UseSqlServer(
        builder.Configuration.GetConnectionString("DefaultConnection")
    );
    options.EnableServiceProviderCaching(false);
});

2. Configure Middleware

app.UseMiddleware<GlobalParameterMiddleware>();

3. Use in Controllers

[HttpGet]
public IActionResult GetAll([FromQuery] string schema)
{
    _parameterService.SetSchemaName(schema);
    using var scope = _serviceProvider.CreateScope();
    var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
    
    var results = context.YourEntity.ToList();
    return Ok(results);
}

🏗️ Architecture

The solution implements a One-layer architecture:

🔑 Key Components

  • GlobalParameterMiddleware: Extracts schema from request headers
  • GlobalParameterService: Manages schema state
  • GlobalParameterActionFilter: Validates schema parameters
  • ApplicationDbContext: Implements dynamic schema switching

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

📝 License

This project is licensed under the MIT License - see the LICENSE file for details

🙏 Acknowledgments

  • Entity Framework Core team for their amazing work
  • The .NET community for their continuous support

📞 Contact

Contact Email: shadykhalifa.dotnetdev@gmail.com

Project Link: https://github.com/shady78/MultiSchema.DbContext.Dynamic


⭐️ If this project helped you, please consider giving it a star on GitHub!

About

Dynamic schema switching implementation for Entity Framework Core, allowing runtime schema changes in a multi-tenant database architecture. Supports middleware, action filters, and schema-based data isolation.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages