All my models are lost, and I can't sleep

Tuesday, August 16, 2016 4:22 PM umbraco

Suddenly, all your nice strongly-typed models generated by Umbraco's models builder seem to have vanished. Every is Completely Broken™, views throw the dreaded Yellow Screen Of Death, reporting a Compilation Error usually around a line looking like:

public class _Page_Views_MyPage_cshtml : Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.MyModel> {

And basically telling you that type ContentModels.MyModel does not exist. And yet it did, a few minutes ago. What went wrong?

Troubleshooting models generation

This situation generally indicates that the models were re-generated—and failed to re-generate. In other words, for some reason, Umbraco thought it had to re-generate the models, and detected an anomaly entirely preventing it to do so, leaving you with no models at all.

Luckily, the models builder reports the last generation errors in the Models Builder tab of the Developer section. There, we see:

Failed to build PureLive models.
The model class for content type with alias "location" is named "Location". 
CSharp does not support using the same name for the property with alias "location". 
Consider using an attribute to assign a different name to the property.

We just added a property named "Location" to our content type named "Location". In terms of models, it means we need to generate a class named Location having a property named Location. Unfortunately, that is a no-no for C#, hence the error message.

There are many other ways to break the models generation, as C# can be picky. Naming a property "System" or "Content", using C# reserved words... Too many, in fact, for us to handle every situation automatically.

But now you know that, when things go wrong, there's a place you can go to understand why.

Names conflicts

In most cases, the generation fails because of names conflicts, such as with the example above.

The easiest way to deal with a name conflict is... to rename the conflicting content type or property. That is: to change its alias. With PureLive, Umbraco will automatically re-generate the models. With Dll, you will need to click the Generate button. Until it all works again.

Alternatively, you can also tell the models builder to use a different name for a property or a content type, See the Models Generation Configuration documentation for more details.

Happy troubleshooting!

comments powered by Disqus