Namespace problem when generating Razor Pages in VS 2019 RRS feed

  • Question

  • User768476531 posted

    In visual studio 2019, when I add a new scaffolded item as "Razor Pages using Entity Framework(CRUD)" after creating a model, I got the right files showed as follows. but in the code lines, the namespace is not right as I want. the namespace does not contain the folds name between the root direction and the files. the expected namespace is as "WorkPoints.Pages.DesignTasks", but the automatically generated namespace is as "WorkPoints".  could anyone know how to get it solved? thanks.

    The file structure generated automatically as follows.

    WorkPoints(the solution name)








    The following code lines show the namespace generated automatically.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using WorkPoints.Data;
    using WorkPoints.Models;
    namespace WorkPoints
        public class CreateModel : PageModel
            private readonly WorkPoints.Data.WorkPointsContext _context;
            public CreateModel(WorkPoints.Data.WorkPointsContext context)
                _context = context;
            public IActionResult OnGet()
            ViewData["DesignPhaseID"] = new SelectList(_context.DesignPhases, "ID", "ID");
                return Page();
            public DesignTask DesignTask { get; set; }
            // To protect from overposting attacks, please enable the specific properties you want to bind to, for
            // more details see https://aka.ms/RazorPagesCRUD.
            public async Task<IActionResult> OnPostAsync()
                if (!ModelState.IsValid)
                    return Page();
                await _context.SaveChangesAsync();
                return RedirectToPage("./Index");

    Tuesday, December 10, 2019 8:40 AM


All replies

  • User475983607 posted

    Use the -nameSpace option when executing the CLI command.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 10, 2019 12:21 PM
  • User711641945 posted

    Hi DavidLIly,

    What is your version of your Visual Studio 2019?Did you use Visual Studio 2019 Preview?If so,I suggest that you could update the Visual Studio.



    And what is the version of .net core sdk, 2.1 or 2.2 or any other version?

    Follow the document to scaffold the razor pages,it would generate the correct namespace by default:


    Best Regards,


    Wednesday, December 11, 2019 3:11 AM
  • User768476531 posted

    Hi Rena,

    Thanks for your reply. My version is Visual Studio 2019 Community and .net core sdk 3.1. But I remember I had used Visual Studio 2019 Preview once before. and got it updated to 2019 community not long ago.

    But now, With Visual Studio 2019 Community and .net core sdk 3.1, and following the document which you sent me to scaffold the razor pages , I still cannot get the right namespace. I still don't know how to work around it but modifying it one by one myself instead.

    How may I do next?

    Thanks again.

    Wednesday, December 11, 2019 7:47 AM
  • User711641945 posted

    Hi DavidLIIy,

    I could not reproduce your issue with .net core 3.1.

    You could choose tab bar:Help->About Visual Studio to check the version of your Visual Studio.And try to build a new project to check if anything missed.

    1.Create a model named DesignTask

    2.Add folder named DesignTasks to Pages folder

    3.right-click `Pages/DesignTasks` folder:

    4.Choose the model and DbContext;

    Best Regards,


    Wednesday, December 11, 2019 9:47 AM
  • User768476531 posted

    Hi Rena,

    I have reinstalled VS2019 Community and updated it to the newest version, and strictly followed the process you said above. But I still cannot get the correct namespace.  I had once installed Visual Studio Code in my system, but now it's uninstalled, is there anything to do with it?

    What you wrote above is correct. I think there may be something wrong with my system.

    Thank you.

    Best Regards,


    Friday, December 13, 2019 1:26 PM
  • User768476531 posted

    Hi Rena,

    By the way, are there any settings in Visual Studio for deciding whether to include the subfolder name in the namespace?


    Best Regards,


    Friday, December 13, 2019 1:42 PM
  • User711641945 posted

    Hi DavidLIIy,


    are there any settings in Visual Studio for deciding whether to include the subfolder name in the namespace?

    I could not find such settings in Visual Studio.Maybe you could reset settings like below:



    It would be the problem with visual studio.I suggest that you could ask Visual Studio Forum for help.

    Best Regards,


    Tuesday, December 17, 2019 2:18 AM
  • User-1296965835 posted

    I have encountered the same improper namespace generation problem while working with the tutorial, "Razor Pages with Entity Framework Core in ASP.NET Core - Tutorial 6 of 8". This was the first tutorial in the series where I experienced problems while following (exactly) the tutorial instructions.

    I am using Version 16.4.1 of Visual Studio, Windows 10.

    After adding a New Scaffolded Item for the Course entity, upon the attempt to build the project I got messages like "CreateModel already has a definition in namespace ContosoUniversity". 

    After a bit of digging I discovered that the scaffolding operation for "Razor Pages Using Entity Framework (CRUD)" generates the namespaces improperly for the Create, Edit, Delete, Details, and Index pages (where it generates only "ContosoUniversity" instead of "ContosoUniversity.Pages.Students", etc., as the namespace).

    This didn't create a problem until Part 6 of the tutorial, because there one employs the scaffolding tool for the second time in the tutorial, to generate pages for Courses. (Pages were earlier generated for Students.) The pages for Courses and Students conflict because of the namespace issue.

    Sunday, December 22, 2019 5:07 PM
  • User1545552040 posted

    Hi David/Manonash.

    Im having the same issue too... Everytime i add a new razor page (not scaffolded item but just a single blank razor page), the namespace isn't correct.

    It seems the folder structure is not being included. I have to manually correct the namespace of the page everytime to include the folder structure or else the namespace would clash.

    Im using Visual studio 2019 and .net core 2.2.  I'm not having this kind of error before but 2 days ago, i reformatted my machine and reinstalled VS2019... and now the issue is happening. 

    Does anyone have a solution on this? 

    Many thanks.

    Sunday, December 29, 2019 1:54 PM
  • User267683364 posted

    I am also having this issue, it definitely seems like a bug with VS 2019. I upgraded to Community 2019 after a clean installation of windows, and have changed minimal settings, so I doubt I have caused this.

    I would be very interested in hearing a solution as it is quite tedious having to fix this manually.

    Sunday, January 5, 2020 4:39 PM
  • User470914704 posted

    Hello David

    In December I was using scafolding with areas, and the namespaces were all being inserted correctly; but after a few VS updates, the auto-generated namespaces are ignoring the directory structure when I use scafolding, both with CRUD-sets and individual pages.

    However I do not like the default namespaces and always remove the 'Areas' and 'Pages' parts anyway.

    So you are experiencing a VS bug.

    It would be nice to be able to define the auto-generated namespaces to save me editing all of mine.

    Monday, January 13, 2020 4:26 PM
  • User1545552040 posted

    Hi everyone,

    Just want to confirm... This issue is still not fixed on VS2019 16.4.3 right? Because Im still getting the incorrect namespace.


    Thursday, January 16, 2020 8:00 AM
  • User-66306004 posted

    Only work around I've found was too right click->Add a Razor Page, Select Razor Entity Framework. This will at least allow creation of each CRUD one at a time without namespace conflict until a fix is in place. 

    Tuesday, January 28, 2020 6:24 AM
  • User-66306004 posted

    Just noticed creating razor crud pages separately only causes link creation problems that need to be addressed. Just easier at this point to copy/paste the namespace after build to each class. Surprised this hasn't been fix.

    Tuesday, January 28, 2020 6:31 AM
  • User-1563099939 posted

    I am having the same problem. This is very basic functionality that I have been using for several version in .net core 2. It seems to have broken in .net core 3. I hope this gets fixed soon because it taking away efficiency of scaffolding.

    Thursday, February 6, 2020 2:16 AM
  • User-821857111 posted

    It's a bug in VS. It won't be fixed until version 16.5 comes out: https://developercommunity.visualstudio.com/content/problem/822390/scaffolding-incorrect-namespace.html

    In the meantime, either manually amend the generated namespaces to reflect the folder structure, or use the CLI to generate the pages.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 18, 2020 3:40 PM