dotnet / dotnet/Scaffolding

CLI scaffolder generates DbContext class without a namespace but VS scaffold UI does.

Open
#2,104 2 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

Either a behavior has intentionally changed with the scaffold tool release for .NET 7 and I missed it and need to document it, or this is a possible issue with the scaffolding tool:

**Expected behavior - quick summary:**
Running the scaffolding tool at the command line on a new asp.net core web app project and specifying `-d` name, results in a generated `DbContext` class with a namespace reflecting that `-dc` name.

``` DotNet Cli
dotnet aspnet-codegenerator razorpage -m Movie -dc RazorPagesMovieContext -udl -outDir Pages\Movies --referenceScriptLibraries -sqlite
```

Also expected: Running the scaffolding tool at the command line has the same results as running from the scaffolding UI in Visual Studio.

**Actual behavior:**
Running the scaffolding tool at the command line on a new asp.net core web app project and specifying a name for `-dc` results in a generated `DbContext` class by that name, but that does not declare a namespace.

```C#
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; } = default!;
}

```

Visual Studio for Windows using the scaffolding UI does, however, declare a `namespace` in the `DbContext` class as expected. In this case, based on the default specified database context name suggested by the UI:

```C#
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; } = default!;
}
}

```

**Environment:**

.NET SDK:
Version: 7.0.100
Commit: e12b7af219

.NET runtime:
Microsoft.AspNetCore.App 7.0.0
Microsoft.NETCore.App 7.0.0

OS:
Windows 10.0.19045, win10-x64

**Steps to recreate issue:**

With the latest .NET SDK installed (Environment detail noted above):

1. Run the following command to create a new ASP.NET web app.
```.NET CLI
dotnet new webapp -o RazorPagesMovie
```

2. Create a model class: Models/Movie.cs with the following code:

```C#
using System.ComponentModel.DataAnnotations;

namespace RazorPagesMovie.Models;

public class Movie
{
public int Id { get; set; }
public string? Title { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
public string? Genre { get; set; }
public decimal Price { get; set; }
}
```
3. Add NuGet packages and EF tools. Verify the latest global tools are used by uninstalling first then reinstalling:

```DotNet CLI
dotnet tool uninstall --global dotnet-aspnet-codegenerator
dotnet tool install --global dotnet-aspnet-codegenerator
dotnet tool uninstall --global dotnet-ef
dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SQLite
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
```

4. Scaffold the model:
``` DotNet CLI
dotnet aspnet-codegenerator razorpage -m Movie -dc RazorPagesMovieContext -udl -outDir Pages\Movies --referenceScriptLibraries -sqlite
```

Result: CLI scaffold tool output does not declare the `namespace` in the `DbContext` class:

```C#
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; } = default!;
}

```

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.