dotnet / AspNetCore.Docs

Documentation for ASP.NET Core
https://docs.microsoft.com/aspnet/core
Creative Commons Attribution 4.0 International
12.61k stars 25.3k forks source link

namespace MvcMovie.Data not generated by dotnet-aspnet-codegenerator command #25307

Closed ethanhou-MSFT closed 2 years ago

ethanhou-MSFT commented 2 years ago

Hi,

I'm trying to follow the guideline using VScode but failed to continue on the MVC chapter part5.

The result of this command is different from the result in visual studio:

namespace MvcMovie.Data will not be generated in Data/MvcMovieContext.cs and using MvcMovie.Data; will not be generated in program.cs neither if I use the above command, it will also block me from following the guideline of next chapter since some compiling issues will be introduced to the project.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Rick-Anderson commented 2 years ago

What NS is generated? You need to run this command from the project folder.

ethanhou-MSFT commented 2 years ago

What NS is generated? You need to run this command from the project folder.

Hi @Rick-Anderson ,

Thanks for your reply. I can guarantee I run that command in the project folder with Program.cs.

No namespace was generated in the MvsMovieContext.cs, I know it sounds weird but here is what I got:

Data\MvcMovieContext.cs

#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using MvcMovie.Models;

    public class MvcMovieContext : DbContext
    {
        public MvcMovieContext (DbContextOptions<MvcMovieContext> options)
            : base(options)
        {
        }

        public DbSet<MvcMovie.Models.Movie> Movie { get; set; }
    }

Program.cs

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<MvcMovieContext>(options =>
    options.UseSqlite(builder.Configuration.GetConnectionString("MvcMovieContext")));

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
Rick-Anderson commented 2 years ago

Try

dotnet tool uninstall -g dotnet-aspnet-codegenerator
dotnet tool install -g dotnet-aspnet-codegenerator
ethanhou-MSFT commented 2 years ago

Try

dotnet tool uninstall -g dotnet-aspnet-codegenerator
dotnet tool install -g dotnet-aspnet-codegenerator

I tried that two commands and get the same result. For your convenience I will paste my log here:

PS C:\Users\ethanhou\repo\MvcMovie> dotnet tool uninstall --global dotnet-aspnet-codegenerator
Tool 'dotnet-aspnet-codegenerator' (version '6.0.2') was successfully uninstalled.
PS C:\Users\ethanhou\repo\MvcMovie> dotnet tool install --global dotnet-aspnet-codegenerator
You can invoke the tool using the following command: dotnet-aspnet-codegenerator
Tool 'dotnet-aspnet-codegenerator' (version '6.0.2') was successfully installed.
PS C:\Users\ethanhou\repo\MvcMovie> dotnet tool uninstall --global dotnet-ef
Tool 'dotnet-ef' (version '6.0.3') was successfully uninstalled.
PS C:\Users\ethanhou\repo\MvcMovie> dotnet tool install --global dotnet-ef
You can invoke the tool using the following command: dotnet-ef
Tool 'dotnet-ef' (version '6.0.3') was successfully installed.
PS C:\Users\ethanhou\repo\MvcMovie> dotnet add package Microsoft.EntityFrameworkCore.Design
  Determining projects to restore...
  Writing C:\Users\ethanhou\AppData\Local\Temp\tmpB87F.tmp
info : Adding PackageReference for package 'Microsoft.EntityFrameworkCore.Design' into project 'C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj'.
info :   GET https://api.nuget.org/v3/registration5-gz-semver2/microsoft.entityframeworkcore.design/index.json
info :   OK https://api.nuget.org/v3/registration5-gz-semver2/microsoft.entityframeworkcore.design/index.json 780ms
info : Restoring packages for C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj...
info : Package 'Microsoft.EntityFrameworkCore.Design' is compatible with all the specified frameworks in project 'C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj'.
info : PackageReference for package 'Microsoft.EntityFrameworkCore.Design' version '6.0.3' added to file 'C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj'.
info : Generating MSBuild file C:\Users\ethanhou\repo\MvcMovie\obj\MvcMovie.csproj.nuget.g.props.
info : Writing assets file to disk. Path: C:\Users\ethanhou\repo\MvcMovie\obj\project.assets.json
log  : Restored C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj (in 119 ms).
PS C:\Users\ethanhou\repo\MvcMovie> dotnet add package Microsoft.EntityFrameworkCore.SQLite
  Determining projects to restore...
  Writing C:\Users\ethanhou\AppData\Local\Temp\tmpD2AE.tmp
info : Adding PackageReference for package 'Microsoft.EntityFrameworkCore.SQLite' into project 'C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj'.
info :   GET https://api.nuget.org/v3/registration5-gz-semver2/microsoft.entityframeworkcore.sqlite/index.json
info :   OK https://api.nuget.org/v3/registration5-gz-semver2/microsoft.entityframeworkcore.sqlite/index.json 837ms
info : Restoring packages for C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj...
info : Package 'Microsoft.EntityFrameworkCore.SQLite' is compatible with all the specified frameworks in project 'C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj'.
info : PackageReference for package 'Microsoft.EntityFrameworkCore.SQLite' version '6.0.3' added to file 'C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj'.
info : Writing assets file to disk. Path: C:\Users\ethanhou\repo\MvcMovie\obj\project.assets.json
log  : Restored C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj (in 161 ms).
PS C:\Users\ethanhou\repo\MvcMovie> dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
  Determining projects to restore...
  Writing C:\Users\ethanhou\AppData\Local\Temp\tmpE675.tmp
info : Adding PackageReference for package 'Microsoft.VisualStudio.Web.CodeGeneration.Design' into project 'C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj'.
info :   GET https://api.nuget.org/v3/registration5-gz-semver2/microsoft.visualstudio.web.codegeneration.design/index.json
info :   OK https://api.nuget.org/v3/registration5-gz-semver2/microsoft.visualstudio.web.codegeneration.design/index.json 905ms
info : Restoring packages for C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj...
info : Package 'Microsoft.VisualStudio.Web.CodeGeneration.Design' is compatible with all the specified frameworks in project 'C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj'.
info : PackageReference for package 'Microsoft.VisualStudio.Web.CodeGeneration.Design' version '6.0.2' added to file 'C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj'.
info : Generating MSBuild file C:\Users\ethanhou\repo\MvcMovie\obj\MvcMovie.csproj.nuget.g.props.
info : Writing assets file to disk. Path: C:\Users\ethanhou\repo\MvcMovie\obj\project.assets.json
log  : Restored C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj (in 880 ms).
PS C:\Users\ethanhou\repo\MvcMovie> dotnet add package Microsoft.EntityFrameworkCore.SqlServer
  Determining projects to restore...
  Writing C:\Users\ethanhou\AppData\Local\Temp\tmpFC20.tmp
info : Adding PackageReference for package 'Microsoft.EntityFrameworkCore.SqlServer' into project 'C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj'.
info :   GET https://api.nuget.org/v3/registration5-gz-semver2/microsoft.entityframeworkcore.sqlserver/index.json
info :   OK https://api.nuget.org/v3/registration5-gz-semver2/microsoft.entityframeworkcore.sqlserver/index.json 902ms
info : Restoring packages for C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj...
info : Package 'Microsoft.EntityFrameworkCore.SqlServer' is compatible with all the specified frameworks in project 'C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj'.
info : PackageReference for package 'Microsoft.EntityFrameworkCore.SqlServer' version '6.0.3' added to file 'C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj'.
info : Writing assets file to disk. Path: C:\Users\ethanhou\repo\MvcMovie\obj\project.assets.json
log  : Restored C:\Users\ethanhou\repo\MvcMovie\MvcMovie.csproj (in 402 ms).
PS C:\Users\ethanhou\repo\MvcMovie> dotnet-aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries -sqlite
Building project ...
Finding the generator 'controller'...
Running the generator 'controller'...

Minimal hosting scenario!
Generating a new DbContext class 'MvcMovieContext'
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.3 initialized 'MvcMovieContext' using provider 'Microsoft.EntityFrameworkCore.Sqlite:6.0.3' with options: None
Added DbContext : '\Data\MvcMovieContext.cs'
Added Controller : '\Controllers\MoviesController.cs'.
Added View : \Views\Movies\Create.cshtml
Added View : \Views\Movies\Edit.cshtml
Added View : \Views\Movies\Details.cshtml
Added View : \Views\Movies\Delete.cshtml
Added View : \Views\Movies\Index.cshtml
RunTime 00:00:13.66

BTW, does that series of commands work for you? If yes, it's okay to close this issue since I can continue with Visual Studio instead of VScode and I just want to make sure the tutorial is up to date :)

MattMorris247 commented 2 years ago

I have this exact same issue. Those 2 lines Rick recommended did not change anything. I cannot continue in the tutorial because it relies on using MvcMovie.Data , however it states the namespace of it does not exist.

ethanhou99 commented 2 years ago

I have this exact same issue. Those 2 lines Rick recommended did not change anything. I cannot continue in the tutorial because it relies on using MvcMovie.Data , however it states the namespace of it does not exist.

Hi @MattMorris247 , yes this is an issue, I don't know why they still close this issue without verifying it. My suggestion is follow the Visual Studio track, at least using Visual Studio to redo the related sections, that will help you skip this issue and I've already tested.

sadullahcihan commented 2 years ago

I am also having the same issue. I could not fixed it yet.

edwestfieldjr commented 2 years ago

I was able to get it to work with these steps:

CREATE TABLE "Movie" (
    "ID"    INTEGER NOT NULL,
    "Title" TEXT,
    "ReleaseDate"   TEXT NOT NULL,
    "Genre" TEXT,
    "Price" TEXT,
    PRIMARY KEY("ID")
)

This is my first go‘round writing in C# and building a .NET MVC App, so I'm going to continue on with this Microsoft tutorial before going back and debugging further to see why the table did not generate on its own.

Current Working Environment:

  • .NET v6.0.401 (macOS)
  • VSCode v1.72.0 (macOS)
  • Chrome v105.0.5195.125 (x86_64)
  • MacOS Monterey 12.6