Search code examples
asp.net-core-mvcasp.net-core-identityasp.net-core-6.0

ASP.NET Core 6 MVC : adding identity failing


I'm using VS 2022, ASP.NET Core 6 MVC and Microsoft SQL Server 2019 (v15).

Git project: [https://github.com/Wizmi24/MVC_BookStore]

I'm trying to add --> new scaffolded item --> identity.

Default layout page, override all files and mine Data context when I click add, I get this error:

There was an error running the selected code generator: 'Package restore failed. Rolling back package changes for 'MyProjectName'

I cleared NuGet Package cache as I saw it may help, but all it do is just prolong and this same error is visible after trying to install Microsoft.EntityFrameworkCore.SqlServer, which is installed. I checked the packages and made sure they are the same version (6.0.11).


Solution

  • I cloned your project to test, and the problem you mentioned did appear. Not sure why, but I finally got it working by updating the NuGet package:

    I updated the two packages Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.Relational to version 7.0.1 (you need to pay attention to the sequence when updating), then add scaffolded Identity, and I succeeded.

    You can try my method, if the Identity is successfully added, but the following exception is encountered at runtime:

    enter image description here

    You need to add builder.Services.AddDbContext<MyBookContext>(); before

    builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
        .AddEntityFrameworkStores<MyBookContext>();
    

    MyBookContext is the Data context class selected when you add Identity:

    enter image description here

    In addition, if there is a 404 error in your area routing, you can refer to this document to modify it.

    Hope this can help you.

    Edit1:

    I think it might be a problem caused by naming duplication. Please try to change the name of the context generated by Identity.

    As you can see, the ApplicationDbContext generated by Identity is consistent with the ApplicationDbContext namespace in your MyBook.DataAccess:

    enter image description here

    So naming the same will cause conflict:

    enter image description here

    So you need to change the naming to avoid conflicts. For example:

    builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(
        builder.Configuration.GetConnectionString("DefaultConnection")
        ));
    
    builder.Services.AddDbContext<ApplicationDbContextIdentity>();
    
    builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
        .AddEntityFrameworkStores<ApplicationDbContextIdentity>();
    

    Edit2:

    As I mentioned in the original answer, if you get a 404 error, you can try to refer to this link to fix the area routing.

    The easiest way is to directly change the routing settings in Program.cs:

    app.MapAreaControllerRoute(
       name: "Customer",
       areaName: "Customer",
       pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
    

    Then add the Area property to the controller:

    [Area("Customer")]
    public class HomeController : Controller{}
    

    There seems to be a problem with your Repository.cs, so I changed the Index to only output a string to test the result.

    public string Index()
    {
        return "success";
    }
    

    Test Result:

    enter image description here

    If your Repository.cs also has problems when you test it, you can make a new post for everyone to help you fix this problem(Because this question strays so far from your original question, one post is better off addressing only one question).

    Good Luck.