I'm trying to implement .net core Identity on an existing system with existing dbcontext. Here is my DbContext class which I made to inherit IdentityDbContext. Basically I want the ASPUsers tables to be in the same database as my application entities. Migration was fine and was able to create the tables successfully, however I am having issues with trying to resolve the dbcontext with the userstore.
DAMDbContext.cs
using DAM.Domain.Entities;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System.Threading.Tasks;
namespace DAM.Persistence
{
public class DAMDBContext : IdentityDbContext, IDbContext
{
public DAMDBContext(DbContextOptions<DAMDBContext> options)
: base(options)
{
}
public DbSet<ApprovalLevel> ApprovalLevels { get; set; }
public DbSet<ApprovalLevelApprover> ApprovalLevelApprovers { get; set; }
public DbSet<Asset> Assets { get; set; }
public DbSet<AssetAudit> AssetAudit { get; set; }
...
Startup.cs
services.AddTransient<ICacheProvider, CacheAsideProvider>();
services.AddTransient<ICacheKeyGenerator, TypePrefixerCacheKeyGenerator>();
services.AddScoped<IAzureStorageService, AzureStorageService>();
services.AddScoped<IConversionService, ConversionService>();
services.AddScoped<IHelperService, HelperService>();
services.AddScoped<IEmailService, EmailService>();
services.AddScoped<ITagService, TagService>();
services.AddMediatR(typeof(GetAssetRequestHandler).Assembly);
services.AddDbContext<IDbContext, DAMDBContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DAMDBConnectionString"),
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.EnableRetryOnFailure();
}));
services.AddIdentity<IdentityUser, IdentityRole>(options =>
{
options.Password.RequiredLength = 10;
options.Password.RequiredUniqueChars = 3;
options.User.RequireUniqueEmail = true;
options.SignIn.RequireConfirmedEmail = true;
}).AddEntityFrameworkStores<DAMDBContext>()
.AddDefaultTokenProviders();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
AppController.cs
private readonly UserManager<IdentityUser> _userManager;
private readonly SignInManager<IdentityUser> _signInManager;
private IEmailService _emailService;
private IConfiguration _configuration;
public AppController(UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager, IEmailService emailService, IConfiguration configuration)
{
_userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
_signInManager = signInManager ?? throw new ArgumentNullException(nameof(signInManager));
_emailService = emailService ?? throw new ArgumentNullException(nameof(emailService));
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
}
// ASP .NET CORE Identity Implementation
[HttpGet]
[Route("Users/{id}")]
public async Task<IActionResult> GetAppUsers(string id)
{
var users = new List<IdentityUser>();
if (string.IsNullOrEmpty(id))
{
users = _userManager.Users.ToList();
}
else
{
users.Add(await _userManager.FindByIdAsync(id));
}
return Ok(new
{
Users = users,
});
}
Issue is when I use the UserManager DI in my controllers I get this issue:
System.InvalidOperationException: Unable to resolve service for type 'DAM.Persistence.DAMDBContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9[Microsoft.AspNetCore.Identity.IdentityUser,Microsoft.AspNetCore.Identity.IdentityRole,DAM.Persistence.DAMDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim`1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole`1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin`1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken`1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim`1[System.String]]'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
Any thoughts on what I am lacking/doing wrong? Thanks in advance!
Edit: Nevermind, saw what was causing the issue.
Changed this:
services.AddDbContext<IDbContext, DAMDBContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DAMDBConnectionString"),
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.EnableRetryOnFailure();
}));
to:
services.AddDbContext<DAMDBContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DAMDBConnectionString"),
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.EnableRetryOnFailure();
}));
Turns out IDBContext was messing things up.
question from:
https://stackoverflow.com/questions/65878706/unable-to-resolve-service-for-type-dbcontext-while-attempting-to-activate-micro