AppData ModelsBuilder


3rd May '20


One of the first decisions we make when starting a new Umbraco project is which version of ModelsBuilder to use. As we build websites in Visual Studio and prefer the Strongly Typed approach we've always used 'Dll' mode. At the time of making this decision as a default a few years ago, it was the recommended approach (based on official Umbraco training). A lot can change in 'a few years' so we have now revisited this decision.

The main reason for this revist is because of the blog post [https://umbraco.com/blog/umbraco-85-release/] - in short ModelsBuilder which is a 3rd party package has been bundled with the Umbraco source code since 2016 - Version 7.4 to be exact. This package has now been removed from Umbraco 8.5 and replaced with a much cutback version directly into the Core. This is a breaking change for us, the solution is to install the ModelsBuilder package via nuget to keep using the package as we always have done. You can get this here [https://github.com/OurModelsBuilder/Our.ModelsBuilder].

As 'AppData' is included in the Core version of ModelsBuilder we decided this may be a better fit for us going forward. (Some say it always was, hindsight is great). To enable 'AppData' you need to add the following to the web.config.

<add key="Umbraco.ModelsBuilder.ModelsMode" value="AppData" />

By default, that is all you need to do. The models when generated will live inside App_Data/Models. If like us you prefer to have more control on how things are automatically generated, there are some settings you can do. Such as changing the namespace of the Models and changing the location the Models are saved. We like to use a 'Core' project for custom controllers, extensions etc - so this would be a perfect place to add our models. The code for this is also set in the web.config.

<add key="Umbraco.ModelsBuilder.ModelsDirectory" value="~/../UnitStack.Core/Models/" />
<add key="Umbraco.ModelsBuilder.ModelsNamespace" value="UnitStack.Core.Models" />
<add key="Umbraco.ModelsBuilder.AcceptUnsafeModelsDirectory" value="true" />

So there we go, this is what we'll be doing for all projects going forward. We prefer the AppData approach now over Dll and wish we had done it before. Having the ability to change the namespace of the models and the location is great. Not to mention the headache with CI with the Dll mode, but that's another story.