Code Generation - lobodava/artisan-orm GitHub Wiki
Artisan.Orm ships with a T-SQL helper script that prints a Mapper class skeleton and a model class for any table or view in your database. Run it once when adding a new entity, paste the output into your project, then customise.
How to run it
- Open the script in SQL Server Management Studio or in Visual Studio's T-SQL editor.
- Point the query window at your database.
- Edit the declared variables at the top of the script — pick the target table or view name, the C# namespace, and any output preferences.
- Execute. The script reads the table's metadata from
INFORMATION_SCHEMA/sysviews; it does not modify any database objects. - The generated code appears in the Messages pane.
What gets generated
- A C# class matching the table's columns, with
int/string/DateTime?/ etc. property types derived from the SQL column types and nullability. - A static
*Mapperclass withCreateObject,CreateObjectRow,CreateDataTableandCreateDataRow. - The corresponding
[MapperFor(typeof(...))]attribute. - A user-defined table type definition that matches the
CreateDataTableshape — paste this into your database project as a starting point.
What you still need to write
- Stored procedures (read / save / delete / business operations).
- Repository methods that wire the procedures together.
- Validation logic — see Artisan Way of DataReply.
- Tests.
Caveats
The generator is a pragmatic SQL script, not a full source generator. Treat the output as a starting point:
- Review nullable-vs-non-nullable distinctions on each property — SQL
null-able columns becomeint?/string?, but the meaning may differ. - For tables with computed columns or columns with strange types (
hierarchyid,geography, custom UDTs), the generated mapping may need hand-edits. - Decimals get rendered with their SQL precision/scale — double-check those match what you actually want in C#.
The script is also a good starting point for your own variations — for instance, generating record types instead of classes, or producing only the model without the mapper.
See also: