
Product
Introducing Scala and Kotlin Support in Socket
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
A powerful .NET library for database-driven code generation. Generate models, repositories, and APIs from your database schema.
EzDbCodeGen now works as a local tool.
Easy code generation based on a database schema given by EZDbSchema. The template language this application uses is HandleBars.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system. This nuget package will dump the published cli package for code generation and a powershell script to run it. The nuget package will dump everything you need for code generation into the project you have selected under the EzDbCodeGen folder.
NOTE: If you have not set your powershell execution remote policy first, you will need to do this as noted in Powershell Execution Policy
dotnet new tool-manifest
dotnet tool install EzDbCodeGen.Cli --interactive
dotnet tool update EzDbCodeGen.Cli --interactive
)dotnet ezdbcg
The EzDbCodeGen CLI tool (ezdbcg
) supports the following command-line options:
Usage: ezdbcg [options]
Options:
-a|--app-name <NAME> Application name (default: "MyApp")
-s|--schema-name <NAME> Schema name (default: "MySchema")
-t|--template <PATH> Template file or directory path
-c|--connection-string <CS> Database connection string
-v|--verbose Enable verbose output
-o|--output <PATH> Output directory path
--save-settings Save current settings for future use
-?|-h|--help Show help information
Basic usage with prompts:
dotnet ezdbcg
This will prompt you for any required information that isn't provided.
Generate code with all parameters:
dotnet ezdbcg -a MyApplication -s dbo -t ./Templates/Entity.hbs -c "Server=localhost;Database=MyDB;Trusted_Connection=True;" -o ./Output -v
Save settings for future use:
dotnet ezdbcg -a MyApplication -s dbo -c "Server=localhost;Database=MyDB;Trusted_Connection=True;" --save-settings
The --test-connection
parameter allows you to validate your database connection string before running the code generation process. The tool will:
The connection test will provide specific feedback for different failure scenarios:
Each error will return a specific error code and descriptive message to help diagnose and fix the issue.
# Basic usage
ezdbcg -t <template-path> -c <connection-string> -o <output-path>
# Test database connection
ezdbcg --test-connection -c "Server=localhost;Database=MyDb;User Id=sa;Password=****"
# Additional options
ezdbcg -t <template-path> \
-c <connection-string> \
-o <output-path> \
-a MyApp \ # Application name
-s MySchema \ # Schema name
-v \ # Verbose output
--save-settings # Save settings for future use
For testing purposes, EzDbCodeGen supports an in-memory mode that doesn't require a real database connection. This is particularly useful for unit tests and CI/CD pipelines.
var command = new CommandMainTestable
{
UseInMemoryStorage = true,
AppName = "TestApp",
SchemaName = "TestSchema",
TemplateFileNameOrPath = "path/to/template.hbs",
ConnectionString = "Server=localhost;Database=TestDB;User Id=test;Password=test;"
};
// Set up mock database
command.SetMockDatabase(mockDatabase);
// Add template to in-memory storage
command.InMemoryFiles[command.TemplateFileNameOrPath] = "{{AppName}}";
// Execute
var result = command.TestOnExecute();
EzDbCodeGen now supports Entity Framework Core code generation with improved templates and handling of entity relationships. You can generate EF Core models and controllers with proper navigation properties and relationship handling.
Example usage:
// Create a configuration
var configuration = new Configuration();
configuration.SetValue("Namespace", "MyApp");
// Set up entity security (optional)
var entitySecurity = new Dictionary<string, EntitySecurity>
{
{ "Customer", new EntitySecurity { Secured = true } }
};
configuration.SetValue("EntitySecurity", entitySecurity);
// Create a code generator
var codeGenerator = new CodeGenerator(configuration);
// Process templates
codeGenerator.ProcessModelTemplate("Templates/EFCoreModel.hbs", templateDataInput, outputPath);
codeGenerator.ProcessControllerTemplate("Templates/EFCoreController.hbs", templateDataInput, outputPath);
EzDbCodeGen is built around the core interfaces from EzDbSchema. Here are the key interfaces you'll work with:
// Root interface for database schema
public interface IDatabase
{
string Name { get; }
string DefaultSchema { get; }
IEntityDictionary<string, IEntity> Entities { get; }
}
// Represents a database table or view
public interface IEntity
{
string TableName { get; }
string TableAlias { get; }
string DatabaseSchema { get; }
IPropertyDictionary<string, IProperty> Properties { get; }
IRelationshipReferenceList Relationships { get; }
}
// Represents a database column
public interface IProperty
{
string PropertyName { get; }
string ColumnName { get; }
string DataType { get; }
bool IsNullable { get; }
bool IsPrimaryKey { get; }
bool IsIdentity { get; }
}
// Represents table relationships
public interface IRelationship
{
string ConstraintName { get; }
string FromTableName { get; }
string ToTableName { get; }
RelationshipMultiplicityType MultiplicityType { get; }
IEntity FromEntity { get; }
IEntity ToEntity { get; }
}
Your Handlebars templates can access these interfaces directly. Here are some common patterns:
{{#each Entities}}
public class {{format "pascalCase" TableName}}
{
{{#each Properties}}
{{#if IsPrimaryKey}}[Key]{{/if}}
{{#if IsRequired}}[Required]{{/if}}
public {{convertType DataType "csharp" IsNullable}} {{format "pascalCase" PropertyName}} { get; set; }
{{/each}}
{{#each Relationships}}
{{#if (eq MultiplicityType "OneToMany")}}
public virtual ICollection<{{format "pascalCase" ToTableName}}> {{format "pascalCase" ToTableName}} { get; set; }
= new List<{{format "pascalCase" ToTableName}}>();
{{else}}
public virtual {{format "pascalCase" ToTableName}} {{format "pascalCase" ToTableName}} { get; set; }
{{/if}}
{{/each}}
}
{{/each}}
For more detailed examples and patterns, see AI_USAGE.md.
EzDbCodeGen Core is a powerful code generation library that leverages Handlebars templates to generate code from database schemas.
String formatting helper with multiple operations:
{{format "camelCase,uppercase" propertyName}}
{{format "snakeCase,tab1" className}}
Supported operations:
Data type conversion helper:
{{convertType sqlType "csharp" nullable}}
{{convertType "varchar(50)" "typescript"}}
Supported target languages:
Documentation generator for properties and relationships:
{{doc property "xml"}}
{{doc relationship "jsdoc"}}
Supported formats:
Template structure manager:
{{#layout}}
{{#region "header"}}
// Auto-generated code
{{/region}}
{{#region "body"}}
public class {{className}} {
{{> properties}}
}
{{/region}}
{{/layout}}
Code formatter for multiple languages:
{{formatCode "csharp" codeString}}
{{formatCode "sql" queryString}}
Supported languages:
This project includes AI support files to help AI assistants understand and work with the codebase:
AI_INDEX.md
: An index of the project structure and key componentsAI_USAGE.md
: Detailed examples and usage patterns for AI assistantsThis project was designed to be hosted and distributed with nuget.com.
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
This project is licensed under the MIT License - see the LICENSE.md file for details
Many thanks to the following projects that have helped in this project
EzDbCodeGen provides a rich set of Handlebars helpers to make template creation easier and more powerful. This guide documents all available custom functions to help you create effective templates.
Helper | Description | Example |
---|---|---|
{{Debugger}} | Adds a debug point (useful for development) | {{Debugger "Checking value"}} |
{{Comma}} | Outputs a comma (useful in template loops) | {{#each Properties}}{{Name}}{{#unless @last}}{{Comma}}{{/unless}}{{/each}} |
{{ToJson value}} | Converts a value to JSON format | {{ToJson Entity}} |
{{ContextAsJson}} | Dumps the current context as JSON | {{ContextAsJson true}} (true for indented) |
Helper | Description | Example |
---|---|---|
{{ToNetType}} | Converts database type to .NET type | {{ToNetType DataType}} |
{{ToJsType}} | Converts database type to JavaScript type | {{ToJsType DataType}} |
{{AsNullableType type isNullable}} | Adds nullable marker (?) if needed | {{AsNullableType "int" IsNullable}} |
Helper | Description | Example |
---|---|---|
{{ToPascalCase}} | Converts string to PascalCase | {{ToPascalCase TableName}} |
{{ToCamelCase}} | Converts string to camelCase | {{ToCamelCase PropertyName}} |
{{ToSnakeCase}} | Converts string to snake_case | {{ToSnakeCase TableName}} |
{{ToSingular}} | Converts plural to singular | {{ToSingular TableName}} |
{{ToPlural}} | Converts singular to plural | {{ToPlural EntityName}} |
{{ToCodeFriendly}} | Makes string code-friendly | {{ToCodeFriendly RawName}} |
{{ExtractTableName}} | Extracts table name from schema.table | {{ExtractTableName "dbo.Customer"}} |
{{ExtractSchemaName}} | Extracts schema name from schema.table | {{ExtractSchemaName "dbo.Customer"}} |
{{AsFormattedName}} | Formats name by removing common suffixes | {{AsFormattedName TableName}} |
{{Prefix prefix value}} | Adds prefix to string | {{Prefix "I" EntityName}} |
{{StringFormat value format1 format2...}} | Applies multiple string formats | {{StringFormat TableName "pascal" "singular"}} |
Helper | Description | Example |
---|---|---|
{{#IfPropertyExists propertyName}} | Checks if property exists | {{#IfPropertyExists "IsNullable"}}...{{else}}...{{/IfPropertyExists}} |
{{#IfFunctionExists functionName}} | Checks if function exists | {{#IfFunctionExists "GetRelationships"}}...{{/IfFunctionExists}} |
{{#IfCond left op right}} | Conditional logic with operators | {{#IfCond Value "==" "string"}}...{{else}}...{{/IfCond}} |
{{#Switch value}}{{#Case 'option'}}...{{/Case}}{{/Switch}} | Switch/case logic | {{#Switch DataType}}{{#Case "int"}}Integer{{/Case}}{{#Default}}String{{/Default}}{{/Switch}} |
Helper | Description | Example |
---|---|---|
{{tsType}} | Converts to TypeScript type | {{tsType DataType}} |
{{tsInterface}} | Formats name as interface | {{tsInterface TableName}} |
{{tsModel}} | Formats name as model | {{tsModel TableName}} |
{{tsService}} | Formats name as service | {{tsService TableName}} |
{{tsComponent}} | Formats name as component | {{tsComponent TableName}} |
{{tsModule}} | Formats name as module | {{tsModule TableName}} |
{{tsRouting}} | Formats name as routing | {{tsRouting TableName}} |
{{tsStore}} | Formats name as store | {{tsStore TableName}} |
Helper | Description | Example |
---|---|---|
{{POCOModelFKProperties}} | Generates POCO model foreign key properties | {{POCOModelFKProperties " "}} |
{{POCOModelFKManyToZeroToOne}} | Generates POCO model many-to-zero-or-one relationships | {{POCOModelFKManyToZeroToOne " "}} |
{{GetForeignKeyProperties}} | Gets foreign key properties for an entity | {{#each (GetForeignKeyProperties Entity)}}...{{/each}} |
{{GetOneToOneReferences}} | Gets one-to-one references | {{#each (GetOneToOneReferences Entity)}}...{{/each}} |
{{GetForeignKeyCollectionsForEntity}} | Gets foreign key collections | {{#each (GetForeignKeyCollectionsForEntity Entity)}}...{{/each}} |
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace {{Namespace}}.Models
{
[Table("{{ExtractTableName TableName}}", Schema = "{{ExtractSchemaName TableName}}")]
public partial class {{ToPascalCase (ToSingular (ExtractTableName TableName))}}
{
public {{ToPascalCase (ToSingular (ExtractTableName TableName))}}()
{
{{#each (GetForeignKeyCollectionsForEntity this)}}
{{NavigationProperty}} = new HashSet<{{RelatedEntity}}>();
{{/each}}
}
{{#each Properties}}
{{#if IsPrimaryKey}}
[Key]
{{/if}}
{{#if IsIdentity}}
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
{{/if}}
{{#if MaxLength}}
[StringLength({{MaxLength}})]
{{/if}}
{{#if IsRequired}}
[Required]
{{/if}}
[Column("{{Name}}")]
public {{AsNullableType (ToNetType DataType) IsNullable}} {{ToPascalCase Name}} { get; set; }
{{/each}}
{{POCOModelFKProperties " "}}
{{#each (GetForeignKeyCollectionsForEntity this)}}
public virtual ICollection<{{RelatedEntity}}> {{NavigationProperty}} { get; set; }
{{/each}}
}
}
export interface {{tsInterface TableName}} {
{{#each Properties}}
{{ToCamelCase Name}}: {{tsType DataType}}{{#if IsNullable}} | null{{/if}};
{{/each}}
{{#each (GetOneToOneReferences this)}}
{{ToCamelCase NavigationProperty}}?: {{tsInterface RelatedEntity}};
{{/each}}
{{#each (GetForeignKeyCollectionsForEntity this)}}
{{ToCamelCase NavigationProperty}}?: {{tsInterface RelatedEntity}}[];
{{/each}}
}
For more detailed examples and usage patterns, refer to the AI_USAGE.md file.
FAQs
A powerful .NET library for database-driven code generation. Generate models, repositories, and APIs from your database schema.
We found that ezdbcodegen demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
Application Security
/Security News
Socket CEO Feross Aboukhadijeh and a16z partner Joel de la Garza discuss vibe coding, AI-driven software development, and how the rise of LLMs, despite their risks, still points toward a more secure and innovative future.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.