Skip to content

CastelloBrancoTecnologia/CBT.EnumToSql

Repository files navigation

CBT.EnumToSql

EnumToSql is a simple library that automatically synchronizes enums in your .NET application with a relational database, particularly SQLite. This is useful when you need to ensure that your database always reflects the values defined in your enums without manual updates.

Features

Automatically creates or updates database tables for enums.

Supports ignoring of specific enum members using [EnumToSqlIgnore].

Provides configurable behavior on mismatches between enum and database values (throw exception or update database).

Default support for SQLite via SqliteFactory, but other databases are supported by passing the appropriate DbProviderFactory.

Installation

Simply reference the library in your .NET project and make sure you have a SQLite or any database available in .net.

At startup, call EnumToSqlService.UpdateAllEnums() to update all enums in database and from any enum in code decorated with [EnumToSqlAttribute] in any assembly that your project use

Example Usage:

 [EnumToSqlAttribute (TableName  = "EnumDeTeste", NameField  = "Sigla", ValueField = "Valor")]   
 public enum EnumDeTeste
 {
        Um = 1,
        Tres = 3,
        Quatro = 4,
        [EnumToSqlIgnore]
        Cinco = 5,            
        Seis = 6,
 }
      
// At startup simple call  EnumToSqlService.UpdateAllEnums () and specify you database provider and connection string

void main () 
{
   // Note that database will only be updated (writed) if enum is diferent in compiled code and values in database.
   EnumToSqlService.UpdateAllEnums(@"Data Source=C:\databases\EnumToSqlTest\EnumDeTeste.db");
}

About

EnumToSql - automatically synchronizes enums with a database

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages