The Sharp Factory
Esc
↑ ↓ to moveEnter to open
Getting Started / Model Generation

Model Generation

Read a database into a model: common settings, the connection, the objects you want, and what the app builds.

10 steps · app screens and real code
On this page

Create Your Model From A Database#

Start The Sharp Factory App and, on the Home page, click New model file. To add another model to a model file you already have, open it and click Add model on the Models page.

Common Settings#

This step only shows when you start a new model file. The Sharp Factory Framework supports multiple models in a single model file, and these settings are shared by all of them. You can change them later on the Settings page, which also has the JSON settings.

  1. Root namespace: The namespace that will be the root for all the C# code generated.

  2. Use root namespace in project name: When checked the names of the class library projects generated are prefixed with the root namespace.

  3. Dependencies folder: The name of the folder under which the code will be generated. The projects will be generated inside this folder in your solution.

  4. Indent size: The number of spaces used to indent the generated C# code.

Click Next.

Common Settings

Connect#

Enter your database server information in this step. You will also name your model here. Please take into account that this is not the connection string that will be used by your application. It is only used by The Sharp Factory App to read your database in order to generate the C# code.

  1. Model name: This is the name of the model in the generated C# code. Remember that you can have multiple models thus giving a meaningful name here is useful to later identify which model you are calling in your code.

  2. Server: The SQL Server where your database resides.

  3. Windows authentication (integrated security): When checked, integrated security (Windows authentication) is used to connect to the database. On macOS and Linux this needs Kerberos to be set up; otherwise turn it off and sign in with a SQL Server user.

  4. User and Password: The SQL Server user and its password. These fields show when integrated security is turned off.

  5. Database: When the connection information is correct click Load and this list is filled with the databases on the server. Choose the one you wish to use in your model. If the server cannot be reached, the reason is shown next to the buttons.

Use test databases: to discover the result columns of a stored procedure, reading a model may have to run the procedure.

Click Next.

Connect: model name, server and database

Choose Objects#

Now it is time to choose the database objects you wish to import into your model. You can choose: Tables, Views, Stored Procedures, Functions, Sequences. The objects appear organized by object type, schema and object name. Check a type or a schema to select everything in it, use Select all or None, and type in the search box to find objects by name.

Click Create model.

Choose Objects

Generating The Model...#

Once you click Create model the Build step shows a progress report. The report stays on screen until you click Done.

Generating Model Progress

Model Details#

Once the model has been generated, you will be taken to the Models page. This page is where you can browse, update and delete the models in your model file, and add more. When your database changes, see Updating The Model. Select a table, view, procedure, function or sequence to see its details: its names in C# and in the database, its columns, keys and navigation properties.

Models page: a table's details

Open the Generated code tab to see the C# that will be generated for it, before anything is written.

Models page: the generated code of a table

Saving The Model#

After generating the model you should click Save in the top bar and choose a folder to save to. We recommend you save your model file inside the solution folder where the code is going to be generated; Sync then finds that solution for you.

You could save the model to a different location if you wish to reuse the model across multiple solutions.

Once the model has been saved the name of the model container file will be shown on the title bar of the window. A dot after the name means there are changes that are not saved yet.

Sync To Your Solution#

Now that you have a model it is time to generate the C# code. The application will create all the projects if they don't exist, add them to the solution and add the references between them. Otherwise, it will refresh or update the code: only the files whose content changed are written, and generated files the model no longer produces are removed. Files you add to the generated projects yourself are never touched. Click Sync in the navigation on the left. Sync generates the code of every model in the model file.

You do not need Visual Studio or any other IDE to be running: Sync writes the project files and the solution file itself. It works with .sln and .slnx solutions.

Choose Target Solution#

Under Sync into, choose the solution to generate the code into. The list shows, in this order:

  1. Solutions this model was synced to before, the most recent first.

  2. Solutions near the model file (.edmj), the nearest first.

Click Browse… to pick any other .sln or .slnx file. To start from scratch, use Or a new solution: choose the folder, the name and the format (.slnx by default) and click Use new solution.

Choose .NET Version#

Next to Change target, choose the .NET version of the generated projects: .NET 10 or .NET 11. Sync creates new projects with it. When you switch it, Sync moves the generated projects already in your solution to that version: each project file shows in the Changes tab, with its diff, before Sync writes it, and only its version changes. The command line prints one line per project.

Your own projects are not changed. A project of yours that references the generated ones needs the same .NET version, or it does not build (see Troubleshooting).

What Sync Would Change#

As soon as a solution is chosen, the Sync page works out what Sync would do, before anything is written: the projects it would create, the references it would add, and every file it would add, change or delete. It works it out again when the model or the target changes. Select a file in the Changes tab to see a diff of it. If you edited files in the solution by hand, click Check again.

Sync#

Click Sync to write the code: the button shows how many files it will write (for example Sync 165 files), and so does the Sync entry in the navigation. The Log tab shows the progress, followed by a report of the files written in each project. After a Sync, a line says when it ran and what it wrote; when the model file has unsaved changes, it also offers Save. If the solution is open in your IDE, the IDE picks up the new and changed projects when it reloads the solution (some IDEs ask first).

Sync From The Command Line#

The sharpfactory command line does the same from a terminal or a build pipeline, using a saved model file:

sharpfactory sync <model.edmj> <solution.sln|.slnx> [--preview]

With --preview, sync writes nothing and prints how many projects, references and files it would add, change or delete; its last line reads Check Completed Successfully: nothing was written. See Installation to download it.

JSON Settings#

Two settings on the Settings page change the generated Entities project for System.Text.Json. Both are off in every model file until you turn them on, and with both off the generated code does not change. Sync applies them like any other change: it lists the files it would write first.

Enable System.Text.Json#

Each database gets a System.Text.Json source generation context in its folder of the Entities project, named after the model: the model AppDb gets AppDbJsonContext. It lists every class of the Entities project (tables, views, and the request and result classes of stored procedures and functions), each also as a list, so System.Text.Json reads and writes them without reflection.

Each class is named with its schema: Sales.Customer is SalesCustomer, and a list of customers ListSalesCustomer. Columns of type xml are written as their XML text.

C#
using System.Text.Json;using MyApp.Entity.AppDb;using MyApp.Entity.AppDb.Sales;var customer = new Customer { Id = 1, Name = "Ana", LastName = "Mora", Balance = 25.50M };// one customer and a list of them, through the context: no reflectionvar json = JsonSerializer.Serialize(customer, AppDbJsonContext.Default.SalesCustomer);var copy = JsonSerializer.Deserialize(json, AppDbJsonContext.Default.SalesCustomer);var list = JsonSerializer.Serialize(new List<Customer> { customer }, AppDbJsonContext.Default.ListSalesCustomer);// or give the context to JsonSerializerOptions oncevar options = new JsonSerializerOptions { TypeInfoResolver = AppDbJsonContext.Default };var same = JsonSerializer.Serialize(customer, options);Console.WriteLine(json);Console.WriteLine(copy!.Name + " " + list.Length + " " + (same == json));

Optimize JSON#

Every property of the Entities project's classes gets a short JSON name with [JsonPropertyName]: A to Z, then A0 to A9, AA to AZ, B0 and so on; the columns in order, then the navigation properties. With both settings on, the customer above is written as:

JSON
{"A":1,"B":"Ana","C":"Mora","D":null,"E":25.50,"F":"Active","G":"0001-01-01T00:00:00","H":null,"I":null}

The model file keeps each name, so a property keeps its name when columns are added or removed: a new column gets the next name, and the name of a removed column is not given again. Sync gives the names to new properties. In the app the model file then has a change to save; the command line saves the model file itself.

Optimize JSON works with or without the context. Whatever reads the JSON needs the same names, so use the generated classes on both ends.

Entity Data Model JSON#

The Sharp Factory Framework uses its own lightweight JSON based format to store the model information. The extension of the file is .edmj. EDMJ stands for Entity Data Model JSON. It is a very lightweight format that ensures high performance for generating new models and parsing existing ones; even with hundreds or thousands of tables (or other database objects).

Next Steps#

Now that you have generated all your code you will want to setup your application to consume the generated code. For more information got to Project Setup. There you will find how to configure your application to start interacting with your database using the generated C# code.

Something wrong or unclear on this page? Tell us on GitHub