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.

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.

  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. Enable 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 that are open in Visual Studio, Rider or VS Code.

  2. Solutions this model was synced to before.

  3. Solutions near the model file (.edmj).

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 this new solution.

Choose .NET Version#

.NET version of new projects sets the target framework of the projects that Sync creates. New projects target .NET 10 or .NET 11 (the "New projects use" choice on the Sync page); projects that already exist in your solution keep the target framework they have.

Preview#

Click Preview to see 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. Select a file in the Changes tab to see a diff of it.

Sync#

Click Sync to write the code. The Log tab shows the progress, followed by a report of the files written in each project. 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 it lists every change it would make and writes nothing. See Installation to download it.

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.