dotnet / dotnet/Scaffolding

dotnet-aspnet-codegenerator fails to generate Data namespace for Context class like in the Microsoft docs (Linux, CLI+VSCode way)

Open
#1,929 0 comments 0 reactions 1 assignee Claimed by @deepchoudhery View on GitHub
area-scaffolding customer-reported
Dominant language
C#
Stars
818
Forks
260
Avg merge
1d 8h
Merged PRs (30d)
10

Description

When I am doing the exact same thing on Linux following the "Visual Studio Code" instructions on this page:
https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model?view=aspnetcore-6.0&tabs=visual-studio-code

So, after creating the project and the Movie model class I run that exact same command:
`dotnet-aspnet-codegenerator razorpage -m Movie -dc RazorPagesMovieContext -udl -outDir Pages/Movies --referenceScriptLibraries -sqlite`

It's output in my terminal:
```
RazorPagesMovieContext -udl -outDir Pages/Movies --referenceScriptLibraries -sqlite
Building project ...
Finding the generator 'razorpage'...
Running the generator 'razorpage'...

Minimal hosting scenario!
Generating a new DbContext class 'RazorPagesMovieContext'
Attempting to compile the application in memory with the added DbContext.
Attempting to figure out the EntityFramework metadata for the model and DbContext: 'Movie'
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 6.0.5 initialized 'RazorPagesMovieContext' using provider 'Microsoft.EntityFrameworkCore.Sqlite:6.0.5' with options: None
Added DbContext : '/Data/RazorPagesMovieContext.cs'
Added Razor Page : /Pages/Movies/Create.cshtml
Added PageModel : /Pages/Movies/Create.cshtml.cs
Added Razor Page : /Pages/Movies/Edit.cshtml
Added PageModel : /Pages/Movies/Edit.cshtml.cs
Added Razor Page : /Pages/Movies/Details.cshtml
Added PageModel : /Pages/Movies/Details.cshtml.cs
Added Razor Page : /Pages/Movies/Delete.cshtml
Added PageModel : /Pages/Movies/Delete.cshtml.cs
Added Razor Page : /Pages/Movies/Index.cshtml
Added PageModel : /Pages/Movies/Index.cshtml.cs
RunTime 00:00:08.10
dotnet-aspnet-codegenerator razorpage -m Movie -dc RazorPagesMovieContext -ud 7,20s user 0,84s system 98% cpu 8,163 total
```

My generated class looks like this (you can see that the intended indents are even generated, but not the surrounding namespace code - strongly suggesting it's really a bug, not just by design):

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using RazorPagesMovie.Models;

public class RazorPagesMovieContext : DbContext
{
public RazorPagesMovieContext (DbContextOptions options)
: base(options)
{
}

public DbSet? Movie { get; set; }
}
```
instead of this:
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using RazorPagesMovie.Models;

namespace RazorPagesMovie.Data
{
public class RazorPagesMovieContext : DbContext
{
public RazorPagesMovieContext(DbContextOptions options)
: base(options)
{
}

public DbSet? Movie { get; set; }
}
}
```

And the `using RazorPagesMovie.Data;` line is also not added to my Program.cs.
I mean, the code works this way, but it's not following the docs and it's inconsistent:
This class is still placed in a newly created "Data" directory.
While my model, like in the docs, is placed in the Models folder and _does_ have a namespace with the folder in it (RazorPagesMovie.Models), which is the way things should be done in general.

OS:
Arch Linux

`dotnet --version`:
`6.0.300`

`dotnet tool list --global`:
```
Package Id Version Commands
-------------------------------------------------------------------------
dotnet-aspnet-codegenerator 6.0.5 dotnet-aspnet-codegenerator
dotnet-ef 6.0.5 dotnet-ef
```

`dotnet list package`:
```
Project 'RazorPagesMovie' has the following package references
[net6.0]:
Top-level Package Requested Resolved
> Microsoft.EntityFrameworkCore.Design 6.0.5 6.0.5
> Microsoft.EntityFrameworkCore.Sqlite 6.0.5 6.0.5
> Microsoft.EntityFrameworkCore.SqlServer 6.0.5 6.0.5
> Microsoft.VisualStudio.Web.CodeGeneration.Design 6.0.5 6.0.5
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.