Search code examples
c#asp.net-mvcasp.net-coreentity-framework-core-2.1

Associate data to default ASP.NET UAM user using Entity Framework Core


Trying to create a simple MVC application that has basic user account management with social account sign-up.

Created ASP.NET Core 2.2 app with individual authentication, added EF core via NuGet (sqlserver and tools). I have built some basic ASP.NET MVC apps and MVC apps in other languages previously.

I got Facebook login to work per this tutorial.

Now I'm lost.

There's a "Data" directory with a bunch of code in it already, and the normal MVC directories. However, I don't see the MVC files that would correlate to any of the pages or actions associated with the account management I can clearly access when I launch the application, including /Identity/Account/Login, /Identity/Account/Manage, /Identity/Account/Manage/SetPassword, etc.

While having all of this functionality up and running in minutes is cool, already I'm at a disadvantage of not knowing how or why it works. Searching for anything I would expect to shed some light on this gives me technical documentation that makes my head spin and provides zero enlightenment.

I want to have a collection of data sets ("Books") associated to each user. One User to many Books. Given that the User (account?) model is not in the Models directory of the MVC application I don't think that's where it's supposed to be added. And if it is, I don't know how to create that association with the User.

This is what I'm seeing in a file called ApplicationDbContextModelSnapshot.cs

namespace Bookshelf.Data.Migrations
    [DbContext(typeof(ApplicationDbContext))]
    partial class ApplicationDbContextModelSnapshot : ModelSnapshot
    {
        protected override void BuildModel(ModelBuilder modelBuilder)
        {
        ...

        modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
            {
                b.Property<string>("Id")
                    .ValueGeneratedOnAdd();

                b.Property<string>("ConcurrencyStamp")
                    .IsConcurrencyToken();

                b.Property<string>("Name")
                    .HasMaxLength(256);

                b.Property<string>("NormalizedName")
                    .HasMaxLength(256);

                b.HasKey("Id");

                b.HasIndex("NormalizedName")
                    .IsUnique()
                    .HasName("RoleNameIndex")
                    .HasFilter("[NormalizedName] IS NOT NULL");

                b.ToTable("AspNetRoles");
            });

... and so on, which looks fairly readable, but does not match up to any of the tutorials I've seen so far.

TL;DR:

Where/how do I correctly create a new models? Is it like this in the Models directory (how I'm used to) or is this obsolete?

Either way, how do I correctly associate data with a user given the baked-in user account management for ASP.NET Core 2.2?


Solution

  • There's a "Data" directory with a bunch of code in it already, and the normal MVC directories. However, I don't see the MVC files that would correlate to any of the pages or actions associated with the account management I can clearly access when I launch the application, including /Identity/Account/Login, /Identity/Account/Manage, /Identity/Account/Manage/SetPassword, etc.

    This is because from ASP.NET Core 2.1 Identity is being provided as Razor Class Library with the ASP.NET Core project templates. If you want to see those Identity related codes and customize then you have to Scaffold Identity in your project.

    Here is more details about this: Scaffold Identity in ASP.NET Core projects

    Moreover if you need ASP.NET Core Identity in MVC format then here is my GitHub Repository where Razor Page Identity has been converted to MVC in ASP.NET Core 2.2.