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);
}
}
}