Hi @Joe
I followed the tutorial and created a sample, everything works well.
In my sample, the Identity's DbContext like this:
public class TestIdentityContext : IdentityDbContext<IdentityUser>
{
public TestIdentityContext(DbContextOptions<TestIdentityContext> options)
: base(options)
{
}
protected TestIdentityContext(DbContextOptions 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);
}
}
The DB first generated DBContext (SecondDbContext) as below: Note: it inherits the Identity's DbContext: TestIdentityContext
public partial class SecondDbContext : TestIdentityContext
{
public SecondDbContext(DbContextOptions<SecondDbContext> options)
: base(options)
{
}
public virtual DbSet<Customer> Customers { get; set; }
public virtual DbSet<Employee> Employees { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Customer>(entity =>
{
entity.ToTable("Customer");
});
modelBuilder.Entity<Employee>(entity =>
{
entity.HasKey(e => e.EmpId);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
In the SecondDbContext's OnModelCreating
method, if we use the following code, it will throw the "Cannot use local variable or local function 'builder' declared in a top-level statement in this context" error. So, for this error, please check your code and make sure you are using the same variable name.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(builder);
Then in the Program.cs file, use the following code to register the DbContext and Service:
builder.Services.AddDbContext<SecondDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<SecondDbContext>();
//register the service
builder.Services.AddTransient<IDataService, DataService>();
The service as below:
public interface IDataService
{
List<Employee> GetEmployees(string gender);
}
public class DataService : IDataService
{
private readonly SecondDbContext _context;
public DataService(SecondDbContext secondDbContext)
{
_context = secondDbContext;
}
public List<Employee> GetEmployees(string gender)
{
var result = new List<Employee>();
if(string.IsNullOrEmpty(gender))
{
result= _context.Employees.ToList();
}
else
{
result = _context.Employees.Where(c => c.Gender == gender).ToList();
}
return result;
}
}
Then in the Privacy.cshtml page:
[Authorize]
public class PrivacyModel : PageModel
{
private readonly ILogger<PrivacyModel> _logger;
private readonly IDataService _dataService;
public PrivacyModel(ILogger<PrivacyModel> logger, IDataService dataService)
{
_logger = logger;
_dataService=dataService;
}
public List<Employee> MyEmployees { get; set; }
public void OnGet()
{
MyEmployees = _dataService.GetEmployees("Male");
}
}
After running the application, the code works well:
Besides, from your code, it seems that there have three DbContexts in your project: AppDbContext
, ApplicationDbContext
and Data_IdentityContext
, please check them carefully. Or you can followed the tutorial and create a sample again. If still not working, please share your project.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion