Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Mar 07, 2018 @ 09:28
    Chriztian Steinmeier
    0

    Rename property in ModelsBuilder

    Hi all,

    I had a problem on a site where a property accidentally had been named the same as its Document Type. This results in the following message on the Models Builder tab in the Developer section:

    Failed to build PureLive models.

    The model class for content type with alias "movie" is named "Movie". CSharp does not support using the same name for the property with alias "movie". Consider using an attribute to assign a different name to the property.

    So what I think its referring to is the RenamePropertyType attribute - but how/where do I actually set this when Models Builder is "down" (i.e., I only have the two files models.err and models.hash in the App_Data/Models/ directory)?

    #halp :)

    /Chriztian

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Mar 10, 2018 @ 11:25
    Marc Goodson
    103

    Hi Chriztian

    The way you use the RenameContentType is a bit of a secret but as long as you promise not to tell anyone else...

    As you've found in the docs,

    https://our.umbraco.org/documentation/reference/templating/modelsbuilder/Control-Generation

    there are ways of applying rules to the way the models are generated, either by creating a public partial class 'with the same name as the class that will be generated' and decorating this partial class with one of the 'class level' instructions eg

      [RenamePropertyType("disqusShortname", "SuperDisqusShortyName")]
        public partial class Blog
        {
          //nothing to see here   
        }
    

    will rename the property with alias disqusShortname with the better named SuperDisqusShortyName in the generated model.

    Some Models Building generating instructions can't be applied at the 'class' level, eg as in your case when you are changing the name of the generated 'class' and so there is the ability to set instructions at the 'assembly level'

    eg

    [assembly: RenameContentType("home", "SuperHome")]
    

    Instructs models builder to generate the class for the home DocType under the name 'SuperHome'...

    .... but where on earth do you type these magic instructions?

    Find the app_data/Models folder

    Create a class file in there, (it matters not what you call the class - but I always call it builder.cs)

    Visual Studio will generate some stub code for you, or you can cut and paste the following:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Umbraco.Web.PublishedContentModels
    {
        public class Builder
        {
        }
    }
    

    Basically, any class in the folder that the models will be generated in will be perused by Models Builder to find instructions for building the Models before the Models get built... so the two examples above you would have in your builder.cs file:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.ModelsBuilder;
    
    [assembly: RenameContentType("home", "SuperHome")]
    namespace Umbraco.Web.PublishedContentModels
    {    
        public class Builder
        {
              //nothing to see here
        }
    
        [RenamePropertyType("disqusShortname", "SuperDisqusShortyName")]
        public partial class Blog
        {
           //nothing to see here
        }
    }
    

    I tend to stuff all the instructions into one file (I know, how messy!) but others may follow the convention of having seperate class files for each Partial Class - it depends on the size and nature of the project... OCD and whether you are extending the classes using Partial Classes anyway etc...

    Anyway, after making this file and saving the changes, restarting your application will regenerate the models, these instructions will be followed and you should end up with your models.generated.cs file taking into consideration your Models building instructions...

    // Content Type 1093 with alias "home"
        /// <summary>Home</summary>
        [PublishedContentModel("home")]
        public partial class SuperHome : PublishedContentModel
        {
    #pragma warning disable 0109 // new is redundant
            public new const string ModelTypeAlias = "home";
    

    and

    ///<summary>
            /// Disqus Shortname: To use comments, you'll need to sign up for Disqus and enter your shortname here (more info: https://help.disqus.com/customer/portal/articles/472097-universal-embed-code)
            ///</summary>
            [ImplementPropertyType("disqusShortname")]
            public string SuperDisqusShortyName
            {
                get { return this.GetPropertyValue<string>("disqusShortname"); }
            }
    

    So in your case the clash will be avoided for the Class Name / Property Name and you WILL end up with a generated models file instead of the error file....

    regards

    Marc

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Mar 10, 2018 @ 14:03
    Chriztian Steinmeier
    2

    Thank you Marc!

    Ace writeup of the bits I'd picked up, and then some!

    I was looking for the Marc as solution button, but I settled for the Mark as solution one - not the same thing, though :-) #sorrymarc

    Cheers!

    Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft