Copied to clipboard

Flag this post as spam?

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


  • Sérgio 16 posts 117 karma points
    Oct 15, 2021 @ 14:55
    Sérgio
    0

    Problem while extending model

    Hello everyone.

    I've been using Umbraco, mostly on version 7. I started getting my feet wet on v9 and some concepts are new to me, of course.

    I have checked out how to extend models at the official documentation: https://our.umbraco.com/documentation/reference/templating/modelsbuilder/Understand-And-Extend

    I've followed those steps and extended a class called HomePage:

    namespace Umbraco.Cms.Web.Common.PublishedModels
    {
        public partial class Homepage
        {
            public int MyProperty { get; set; }
        }
    }
    

    But I am constantly getting this error:

    Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.CompilationFailedException: 'One or more compilation failures occurred:
    x5hui2un.yt1(66,94): error CS0433: The type 'Homepage' exists in both 'ModelsGeneratedAssembly, Version=0.0.0.1, Culture=neutral, PublicKeyToken=null' and 'Umbraco9Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null''
    

    Could anyone be so kind as to shed some light here? I can't pinpoint what I may be doing wrong.

    Thank you very much in advance.

  • Søren Kottal 571 posts 3749 karma points MVP 3x c-trib
    Oct 15, 2021 @ 15:16
    Søren Kottal
    100

    You need to have your generated models, and your partial classes in the same project.

    What are your models builder settings? You can set them in appsettings (https://our.umbraco.com/documentation/Reference/Templating/Modelsbuilder/configuration)

    Make sure your models directory is in the project where you extend the models.

  • Sérgio 16 posts 117 karma points
    Oct 15, 2021 @ 15:41
    Sérgio
    0

    That was the missing link. Nowhere in the documentation that information is present. I was using the default settings (InMemoryAuto) and never remembered or realized that the class would be outside the project.

    I changed it to SourceCodeAuto, updated the models and it's now working.

    Thank you very much, Søren.

Please Sign in or register to post replies

Write your reply to:

Draft