I have a Visual Studio Solution containing three projects. One is DAL (Data Access Layer) witch is using Entity Framework Core with one ApplicationDbContext. I have referenced it in another (the second project) witch is a backend panel that users will be authenticated with active directory and users and roles will be managed with help of ASP.NET Identity.
I have created another ASP.NET Core project (the website - third project) which is for separation of the panel and the website. Here is also referenced DAL project. I want to store users of the website in the same database as the project of the backend panel, but I want separate tables for backend users and website users.
In website project I have created another ApplicationDbContext and in
OnModelCreating I have override the default name of Identity tables with different prefix than
The database connection string is the same for both
ApplicationDbContext. I have done
Add-Migration Inital and then updated the database
Update-Database. I get the new tables with the new prefix in the same database with those with
So far so good.
The problem that I face is that it is not straightforward to query the database with two different contexts.
So my question is: Is it possible to user ASP.NET Identity twice in the same database with the same context?
I am open to accept other solution to achieve what I need.
No. It is not possible to have two separate instances of Identity tables and classes in the same context. The way you're doing it with separate contexts is the best method. If you want to simplify access, I'd recommend creating one or more service-style classes that are injected with both contexts. Then, you can simply inject your service class into your application, instead, and work with just one thing via that. You'll of course need to write methods that interact with your two contexts as necessary, but that's work you'll just need to do once and then you can reap the benefits from there.