dotnet / AspNetCore.Docs

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

Wrong using statement added to Razor Pages work with a database tutorial's Visual Studio Code examples #27614

Closed colinkiama closed 1 year ago

colinkiama commented 1 year ago

Some of the VIsual Studio Code examples contain the following using statement:

using RazorPagesMovie.Data;

This line stops the example from building when going through the tutorial with Visual Studio Code.


Document Details

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


Associated WorkItem - 36499

Rick-Anderson commented 1 year ago

Stops it how? What's wrong with it?

See Files created and updated

The scaffold process creates the following files:

Pages/Movies: Create, Delete, Details, Edit, and Index.
Data/RazorPagesMovieContext.cs
colinkiama commented 1 year ago

On my machine, Data/RazorPagesMovieContext.cs is created without being in the RazorPagesMovie.Data namespace.

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<RazorPagesMovieContext> options)
            : base(options)
        {
        }

        public DbSet<RazorPagesMovie.Models.Movie> Movie { get; set; } = default!;
    }

This doesn't cause any issues until the "work with a Database" Visual Studio Code code examples which starts adding using statements for the RazorPagesMovie.Data namespace.

I think the issue is with the consistency of the code examples for example in this section: https://learn.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/sql?view=aspnetcore-7.0&tabs=visual-studio-code#add-the-seed-initializer

It made me think that I missed a step somewhere because it's not consistent with the code I have in my editor for Program.cs before adding the highlighted code in the Add the seed initializer step:

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

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddDbContext<RazorPagesMovieContext>(options =>
    options.UseSqlite(builder.Configuration.GetConnectionString("RazorPagesMovieContext") ?? throw new InvalidOperationException("Connection string 'RazorPagesMovieContext' not found.")));

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/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.MapRazorPages();

app.Run();
colinkiama commented 1 year ago

Here's an example of the error that occurs on build:

/home/colinkiama/RazorPagesMovie/Models/SeedData.cs(2,23): error CS0234: The type or namespace name 'Data' does not exist in the namespace 'RazorPagesMovie' (are you missing an assembly reference?) [/home/colinkiama/RazorPagesMovie/RazorPagesMovie.csproj]
Rick-Anderson commented 1 year ago

@wadepickett can you investigate ASAP? If CLI scaffolder doesn't add correct namespace, open a scaffolding issue and mention me.

wadepickett commented 1 year ago

@Rick-Anderson,

Following the tutorial as it is published now, on VSC for Windows, you do get the following for Data/RazorPagesMovieContext.cs:

EDIT by @Rick-Anderson add code fencing

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<RazorPagesMovieContext> options)
            : base(options)
        {
        }

        public DbSet<RazorPagesMovie.Models.Movie> Movie { get; set; } = default!;
    }

Worth noting, there are couple of moments when the code shown in the latest VSC tutorial update do not match the project:

For VSC at the top of the work with a database tutorial it says appsettings.json should have "Data Source=RazorPagesMovie.Data.db":

{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "ConnectionStrings": { "RazorPagesMovieContext": "Data Source=RazorPagesMovie.Data.db" } }

But this is what is generated following the steps is "Data Source=.db":

{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "ConnectionStrings": { "RazorPagesMovieContext": "Data Source=.db" } }

Then later creating the seed initializer and updating program.cs: Using RazorPagesMovie.Data; is shown, but not highlighted as one to add. However, "using RazorPagesMovie.Data" does not exist in program.cs.

image
wadepickett commented 1 year ago

@Rick-Anderson Removing: using RazorPagesMovie.Data;

from the SeedData.cs code in steps for Work with a database - Seed the database allows it all to work fine.

I am not catching on to why the connection string is set as "Data Source=.db" plus the RazorPagesMovie.Data namespace is not used. Is that as designed, and we simply need to drop using RazorPagesMovie.Data or is that not intended and I should file a bug? If you are not sure, that's fine, I'll just file it and find out.

Rick-Anderson commented 1 year ago

@wadepickett open a scaffold bug. If it's creating a file outside the root folder without the folder name in the namespace, that's a bug. If it's creating different code than the VS scaffolder, that's a bug.

Use the simplest possible instructions in the repo.

something like

  1. Create a new RP web app in VS, call in ...
  2. Create a model (doesn't need to be in models folder)
  3. Create the Pages/Movies folder
  4. Scaffold

Then have CLI instructions to do the same

wadepickett commented 1 year ago

Ok thanks Rick. Honestly I don’t know where that scaffolding repo is that the bug should go into. Where is it?


From: Rick Anderson @.> Sent: Monday, November 14, 2022 6:36:30 PM To: dotnet/AspNetCore.Docs @.> Cc: Wade Pickett @.>; Mention @.> Subject: Re: [dotnet/AspNetCore.Docs] Wrong using statement added to Razor Pages work with a database tutorial's Visual Studio Code examples (Issue #27614)

@wadepicketthttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fwadepickett&data=05%7C01%7Cwpickett%40microsoft.com%7Cbe3fe136329d46b4f57b08dac6b23492%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638040765935098462%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=JgeH4kJ4NTEB58I5imvyUHXvGsBdnBRUoLNgSDa5JZg%3D&reserved=0 open a scaffold bug. If it's creating a file outside the root folder without the folder name in the namespace, that's a bug.

Use the simplest possible instructions in the repo.

something like

  1. Create a new RP web app in VS, call in ...
  2. Create a model (doesn't need to be in models folder)
  3. Create the Pages/Movies folder
  4. Scaffold

Then have CLI instructions to do the same

— Reply to this email directly, view it on GitHubhttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fdotnet%2FAspNetCore.Docs%2Fissues%2F27614%23issuecomment-1314680236&data=05%7C01%7Cwpickett%40microsoft.com%7Cbe3fe136329d46b4f57b08dac6b23492%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638040765935098462%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=UVGs4Z6TNFB%2FTounmSNSlDIN2z6gyY2kbPqk3EBFrkk%3D&reserved=0, or unsubscribehttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FACTZ66FYWDEFMEHUGF3FBWTWILZK5ANCNFSM6AAAAAAR7E5FMI&data=05%7C01%7Cwpickett%40microsoft.com%7Cbe3fe136329d46b4f57b08dac6b23492%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638040765935098462%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=SpxX8goaqCseWqSq3HYFxYmmbYAzYEnAddQR9RqwfqA%3D&reserved=0. You are receiving this because you were mentioned.Message ID: @.***>

Rick-Anderson commented 1 year ago

https://github.com/dotnet/Scaffolding

wadepickett commented 1 year ago

Also of note: since the db context name is being provided and the cli tool seems to be completely ignoring that on output.

wadepickett commented 1 year ago

Tracking product issue: dotnet/Scaffolding#2104

wadepickett commented 1 year ago

The development team took a look at this issue and they will work on it: https://github.com/dotnet/Scaffolding/issues/2104

In between time the solution is to update the tutorials to specify the namepace with a separator in the -dc in the scaffolding command:

Here is the suggestion provided: The namespace name is added for the DbContext if a . separator is used when specifying the db context, ala, dotnet aspnet-codegenerator razorpage -m Movie -dc **BlahNamespace**.RazorPagesMovieContext -udl -outDir Pages\Movies --referenceScriptLibraries -sqlite

So I will update this tutorial and create issues for others that need it for now and take care of those as well.