What do I use instead of AspNetUsers

Viewed 40

I am trying to use to an ID for my link, so it can display the specific user when their id is entered in the URL.

The way I've been doing it is using

var url = _AU.TableName.FirstOrDefault(p => p.Id == id);

I'm trying to pull it from the AspNetUsers table. I created a model in the identity folder, so I can add the first name, and last name column to the AspNetUsers table.

In the DbContext, it doesn't define the AspNetUsers as in

public DbSet<ApplicationUser> AspNetUsers { get; set; }

Because of that it throws an error:

'ApplicationUser' does not contain a definition for 'AspNetUsers' and no accessible extension method 'AspNetUsers' accepting a first argument of type 'ApplicationUser' could be found (are you missing a using directive or an assembly reference?)

What do I need to use in order to access the columns in the AspNetUsers table in my controller? I can't use

var edit = _AU.AspNetUsers.FirstOrDefault(p => p.Id == id);

Model:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;

namespace Website_friend_feature.Areas.Identity.Data
{
    // Add profile data for application users by adding properties to the ApplicationUser class
    public class ApplicationUser : IdentityUser
    {
        [PersonalData]
        [Column(TypeName = "nvarchar(100)")]
        public string FirstName { get; set; }

        [PersonalData]
        [Column(TypeName = "nvarchar(100)")]
        public string LastName { get; set; }

        [PersonalData]
        [Column(TypeName = "nvarchar(1000)")]
        public string ProfilePicture { get; set; }
    }
}

DbContext:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Website_friend_feature.Areas.Identity.Data;

namespace Website_friend_feature.Data
{
    public class AuthDbContext : IdentityDbContext<ApplicationUser>
    {
        public AuthDbContext(DbContextOptions<AuthDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
        }
    }
}

Controller:

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Website_friend_feature.Areas.Identity.Data;

namespace Website_friend_feature.Controllers
{
    public class UserController : Controller
    {
        private readonly ApplicationUser _AU;

        public UserController (ApplicationUser AU)
        {
            _AU = AU;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Profile(String id) 
        {
            if (id == null)
            {
                return BadRequest();
            }

            var edit = _AU.AspNetUsers.FirstOrDefault(p => p.Id == id);
            {
                return NotFound();
            }

            return View(edit);
        }
    }
}
1 Answers

Actually, you are approaching this in the wrong direction. By default Asp.net identity system provides a built-in management system. This means you should use the classes that are being provided by the identity system.

In your example, you are trying to get a user using an entity framework. While you can easily get a user by using the UserManager class.

so first you need to configure the user manager in the dependency injection in your class:

private readonly UserManager<ApplicationUser> _userManager;

public UserController(UserManager<ApplicationUser> userManager)
{
    _userManager = userManager;
}

And then call FindByIdAsync method to retrieve a single user.

public async Task<IActionResult> Profile(String id) 
{
    //Check if the id is not null
    if (id == null)
    {
        return BadRequest();
    }
    
    //Send async request to the database
    var user = await _userManager.FindByIdAsync(id);

    //Check if the user is not null
    if(user is null)
    {
        return NotFound();
    }

    //Return the user object to the database
    return View(user);
}

The controller is set to async and as a Task<IActionResult> because you are fetching data from the database.

Microsoft docs for identity system: Identity system docs

Related