dotnet / AspNetCore.Docs

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

Asp.net 6 scaffold identity generates additional connection string and register dbcontext and identity twice #26241

Open sxr1999 opened 2 years ago

sxr1999 commented 2 years ago

I create ASP.NET Core Web App with individual user accounts, Then i scaffold identity, Here i select Register, Login, LogOut, and RegisterConfirmation, Then i select ApplicationDbContext as Data Context Class and click add.

image

The problem is that in program.cs it generates additional connection string (so there are two of them), as well as dublicates of builder.Services.AddDbContext... and builder.Services.AddDefaultIdentity...

image

I try this steps in asp.net core 3.1 and it will not show the same situation as .net 6

The version of my VS is Visual Studio Community 2022 (17.1.6).


Document Details

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

Dillion132 commented 2 years ago

I also met the same issue, when I created a new Asp.net 6 and then used the Scaffolding Identity. Everything works fine if I delete (comment out) these newly generated lines.

This error only appeared recently and only on Asp.net 6, it does not happen on Asp.net 5 and the previous version.

coalescing-ascender commented 2 years ago

This problem has been intensified as of the latest build (version 17.2.6). The duplicate connection string gets created in the Program.cs and the ApplicationDbContext is duplicated in the Identity/Data namespace but also a custom Identity is not recognized in the scaffold process. The later was not an issue in version 17.2.5.

Choosing the existing Context from the MVC project Data namespace, whether with a custom Identity or not, would work perfectly in v. 17.2.5 but no longer in v. 17.2.6

bsimser commented 2 years ago

I think I'm experiencing a similar problem but the numbers are different. I'm assuming you guys are referring to the version of Visual Studio when it comes to 17.x rather than versions of packages.

I'm using Visual Studio 17.2.3 and getting a duplicate ApplicationDbContext whenever I add any Identity scaffolding items. The main one (created with the initial project) is in Data/ApplicationDbContext.cs but after scaffolding any Identity overrides (even selecting the dbcontext) it creates a different one in Areas/Identity/Data/ApplicationDbContext.cs

To reproduce this:

  1. Create a new project with Visual Studio 17.2.3
  2. Select Individual Accounts on creation
  3. Run the initial migration to get the database setup
  4. Add an Identity scaffolded item to the project (I initially chose EnableAuthenticator to add QR codes but any override will work)
  5. Select your existing ApplicationDbContext class for the data
  6. Click Add

This creates the additional ApplicationDbContext class in the Identity area so you manually have to merge it with your existing one or remove it (if it's the same).

Visual Studio 2022 17.2.3 Microsoft .NET Framework 4.8.04161

I'm going to try updating VS2022 if there's one available as the last comment mentioned 17.2.5 and 17.2.6 but it's not clear what product/package this is referring to.

UPDATE: I upgraded to 17.2.6 and created a new project and the duplication happens. Going to log a bug on the aspnetcore project for this.

PrimusInterParess commented 2 years ago

I'm hitting the same duplication problem, when scaffolding Identity. VS 17.3.6 Enterprise/.net6.0 . Is there a solution ?