Since Umbraco released the models builder, they have speeded up the building of websites and as the code generated creates the document type and properties for us, this allows us to have intellisense in Visual Studio allowing for strongly types models.
Since the models builder has all it classes auto-generated in visual studio, this means that we cannot alter the properties within the class as they will be overridden on the next update.
As you can see in the example code above, it warns you about modify the code.
Changes to this file will be lost if the code is regenerated.
So how do we add our own code to the partial class?
Fortunately, the class is partial, this means that we can create our own class within the same namespace and also make that partial.
Therefore say we do not add width to the image in the CMS, we can now create a property called ImageDefaultWidth and add a default value to the image width. Now, all we need to do is instead of displaying imageWidth, we can display the value of ImageDefaultValue.
Although this was a simple example, I hope it shows you how easy it is to extend partial classes when working with visual studio and the Umbraco models builder.
If you found this post helpful, please leave a comment below.