If you're working with an Umbraco site using Visual Studio you will more than likely benefit from using the API version of Umbraco's Model Builder.
The API mode allows the generated models to be included in the project, shared easily with other developers via source control and can be referenced by other projects in your solution. There are other modes of Umbraco Models Builder which I won't go into here, see the Umbraco Documentation for more detail.
Within the web.config there are two ModelsBuilder app settings. These need to be set to the following:
<add key="Umbraco.ModelsBuilder.Enable" value="true" /> <add key="Umbraco.ModelsBuilder.ModelsMode" value="Nothing" />
The Enable is self explanitory and setting the ModelsMode to Nothing means that there will not be an option to generate the models within Umbraco's Developer tab. As we're going to be generating the models through the ModelsBuilder API and including the generated models directly into our source code, there is no need to generate the models via Umbraco anymore. Setting the ModelsMode to Nothing removes the option and removes any confusion.
We need to install a NuGet package in order to use the ModelsBuilder API. In Visual Studio, open the Package Manager Console. This can be found in Tools -> NuGet Package Manager -> Package Manager Console.
You will need to select your default project to you Umbraco web project and may need to choose nuget.org as your package source instead of All.
Then you're set to run the following command to install the package.
Install Visual Studio extension
With the Umbraco.ModelsBuilder.Api package installed we're now able to call the Umbraco API that will provide the details of the Models to be generated but we need a tool that will actually generate these models for us. In steps the Umbraco ModelsBuilder Custom Tool extension by ZpqrtBnk, the source of which can be found on GitHub.
You can either install the extension using the link above or through Visual Studio, Tools -> Extensions and Updates...
You will need to close all instances of Visual Studio to trigger the actual installation of the extension.
Configuring the extension
Now if we navigate to Tools -> Options and scroll down we'll see we have an Umbraco option with three fields. We need to populate these to tell the extension how to call the Umbraco ModelsBuilder API so it can generate the models.
This needs to be the url of the Umbraco site we're going to generate our Models for. Usually in development this would be localhost or whatever url you're using.
This is the admin Umbraco username used to login.
This is the admin Umbraco password used to login.
Add and configure ModelsBuilder class
As we are going to be generating the Models directly in our code we need to add a class that will be used to trigger the build and also somewhere to store the generated model classes.
This class can be named anything and placed wherever makes sense for your particular project. For this example we've created a ModelsBuilder.cs class in a Models folder within the Umbraco web project.
Now we need to configure this class to be able to use the Umbraco ModelsBuilder Custom Tool extension so we can trigger the generation of the Models. To so so right click the ModelsBuilder.cs and select Properties (or Alt-Enter as a shortcut).
Within the Custom Tool field type in 'UmbracoModelsBuilder'.
Trigger the ModelsBuilder
Now we've installed and configured everything we can actually generate the models. This is done by right clicking on the ModelsBuilder.cs file and selecting Run Custom Tool. This will tell the extension to contact the Umbraco site's ModelsBuilder API and generate the models for us as cs files.
NB: If the website is localhost or similar you will need to have the project running
This process should only take a few moments and upon completion we can see our generated models nested below ModelsBuilder.cs.
These generated classes should also be included into source control so that others on your team have access without having to go through all the above steps. Any time the models change in the future they only need to be generated once and not by each developer every time.
Referencing the generated models
You can reference these models in your code as you would with any other class. You can check the namespace by viewing one of the generated models but it will be something along the lines of MyWebsite.Models.GeneratedModel where MyWebsite is your web project name and GeneratedModel is the name of model you're wanting to reference (if you created these models in your web project).
I also like to add the generated models namespace to our Views web.config so that we can reference the models anywhere in our views without needing to state the full namespace each time.
To do so add the following to the namepspaces section of the views web.config located in the root of the Views folder. Again, replace MyWebsite with your correct web project reference.
<add namespace="MyWebsite.Models" />
With this added you can now reference any of the generated models directly in the view.
You are now all set with the ModelsBuilder API and no longer have to generate these models within Umbraco which can cause all sorts of problems when you reference these models in compiled code.
Happy models building!