Home > Net >  Extend Identity User not working in Asp.Net core 6
Extend Identity User not working in Asp.Net core 6

Time:12-24

I'm building website by Unit of work & repository pattern with built in dependency injection in in .Net core 6.

I've successfully get database data for all entities by using unit of work.I implemented Application User by Identity User to add more fields into Asp.Net Users table.

But when I call

ar users = _userRepository.GetAll().ToList();
var users2 = _unitOfWork.ApplicationUsers.GetAll();

Then users , users2 are null.

`** ``public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IUnitOfWork _unitOfWork;
        private IApplicationUserRepository _userRepository;

        public HomeController(ILogger<HomeController> logger, IUnitOfWork unitOfWork,                IApplicationUserRepository userRepository)
        {
            _logger = logger;
            _unitOfWork = unitOfWork;
            _userRepository = userRepository;
        }

        public IActionResult Index()
        {
            var users = _userRepository.GetAll().ToList();
           
            return View(users);
        }

}`**

**public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
           : base(options)
        {

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


}

public class ApplicationUser : IdentityUser
    {
        [Required]
        public string Name { get; set; }
        public string? StreetAddress { get; set; }

    }**

**

public class ApplicationUserRepository : Repository<ApplicationUser>, IApplicationUserRepository
    {
        private readonly ApplicationDbContext _db;
        public ApplicationUserRepository(ApplicationDbContext db):base(db)
        {
            _db = db;
        }
    }
 public interface IApplicationUserRepository : IRepository<ApplicationUser>
    {
    }

**

public interface IUnitOfWork
    {
      
        IApplicationUserRepository ApplicationUsers { get; }

      }


  public class UnitOfWork : IUnitOfWork
    {
        private ApplicationDbContext _db;

 public IApplicationUserRepository ApplicationUsers { get; private set; } 

   public UnitOfWork(ApplicationDbContext db)
        {
            _db = db;

  ApplicationUsers = new ApplicationUserRepository(_db);

}

builder.Services.AddIdentity<IdentityUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddDefaultTokenProviders()
    .AddEntityFrameworkStores<ApplicationDbContext>();

builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddScoped<IApplicationUserRepository, ApplicationUserRepository>();


builder.Services.AddDbContextPool<ApplicationDbContext>(options => options.UseSqlServer(connectionString));

**I try with Unit of work & also with IApplicationRepository but Application User not providing users data. 
I want to extend identity user and want to use ApplicationUser repository by implementing custom methods to get data. Thanks.**



CodePudding user response:

I want to extend identity user and want to use ApplicationUser repository by implementing custom methods to get data

Well, based your code shippet, it appeares that, your ApplicationDbContext implementation is incorrect. We should implement ApplicationUser within IdentityDbContext like this IdentityDbContext<ApplicationUser> instead of ApplicationDbContext : IdentityDbContext because if you do so, ApplicationDbContext wouldn't know about the newly inherited ApplicationUser class. Therefore, please modify your code as following.

Inherit your IdentityUser class in ApplicationUser Class:

public class ApplicationUser : IdentityUser 
        
        {
           [Required]
           public string Name { get; set; }
           public string? StreetAddress { get; set; }
    
        }

Then Inherit your IdentityDbContext class in your ApplicationDbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }

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

}

Update program.cs:

builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();

Note: If you still remain any confusion please refer to our offical document here.

  • Related