AspNet Identity ApplicationDbContext on existing database - asp.net

I have an existing database with my application tables and i am about to build a new version for my application using MVC5. I decided to use AspNet Identity framework as part of my application.
The visual studio template i used while creating the project added a file "IdentityModel.cs" and the class
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
}
So i can access the tables Users, Roles and other AspNet Identity tables using the code like:
var context = new ApplicationDbContext();
context.Users.ToList();
Because of the name Microsoft gave to the class "ApplicationDbContext" (and not: IdentityDbContext for example), i am wondering if that class should be used as "accessor" for all the rest existing tables that not related to AspNet Identity framework or not?
without the so generic name of the class "ApplicationDbContext" i would just use my entity framework project i just added to my solution to access the other tables of my application but i am wondering what is the "best practices". To use the same AspNet Identity ApplicationDbContext accessor (and how?) or to work with two Db accessors, one of the AspNet Identity tables and one for the entity framework i created for the rest of the tables (Db First).
it looks me more logic to use the same dbContext for all the tables, AspNet identity and all the rest of the tables i have in a separate EntityFramework edmx file. How can i use both of them in one dbContext?

AspNet Identity provide something like user manager, it should solve your problem. You don't need to operate on real tables, AspNet Identity should cover table structure and should allow to do high level operations. Your user model should inherit IdentityModel class, then you are able to build your own custom model.

It sounds like part of your issue is that while IdentityFramework is using code-first entity framework you are not using code-first for the rest of your application, instead using the older edmx designer approach. I would not try to mix the two into a single context.
Moreover, because ApplicationDbContext inherits from IdentityDbContext I would leave it alone. The base class implements things like OnModelCreating and ValidateEntity. If you wanted to use your other context for IdentityFramework you'd need to either have your other context inherit from IdentityDbContext--which is bad semantics, because it is not in fact just an identity context--or you'd need to implement these methods manually.
It is easy, however, to point the identity framework context to the same database as your other entity framework items. Just replace "DefaultConnection" in your ApplicationDbContext with whatever the name of the connection string you use for your other stuff is.

Related

Data context Class Missing in Identity

So I have a ASP Core 3.1 project and have managed to scaffold out via EF all the rest of my existing tables from my SQL server.
These all work fine and do as i need them to.
However when i go to add Identity to the project using my existing ASP User tables i cannot find the Data context class in the Scaffold menu.
I have already tried re-loading Identity, changing the base, and changing the startup file to look at the current database connection
Any help would be appreciated
I had the same problem. I was inheriting the superclass DbContext in my ApplicationDbContext. Since I was using Identity platform, changed it to IdentityDbContext. It fixed my issue.

Can I Use DbContext for creating asp.net identity user tables instead IdentityDbContext

I want to make identity user tables in my asp.net core web API project and I want to make it with code first approach that's why i want to use DbContext as compared to IdentityDbContext. Can I do this? If Yes, then how?

ASP.NET Core Entity Framework identity discriminator cannot be NULL

I'm moving an application from ASP.NET to Core is driving me nuts. A discriminator column has been added to the AspNetUsers table. The app is using EF Core 5 code first and I understand this has something to do with Table Inheritance which we do not want.
Well this is really nasty as can not access the property from my controller as it seems to want to be the calling class (ApplicationUser) or even set the SQL type to 'allow NULL'.

Onion Architecture Identity Framework

I am following Onion Architecture. And in that I am using ASP.NET Identity Framework.
Here is my Project Structure:
1-Core
- Domain Classes //It contains my T4 template classes
-- AppUser //It is Identity User.
- Repository Interfaces
- Service Interfaces
2-Infrastructure
- Data //It contains my edmx file, I am using Db First approach.
- Dependency Injection
- Repository Interfaces Implementation
- Service Interfaces Implementation
3-WebApi
- Web Api Project
4-WebClient
- My AngularJs App
5-Test
- Test Project
I have copied the scripts of the ASP.NET Identity tables and executed on my SQL Server. Which created Identity Tables for me.
In my Infrastructure.Data project, I created an Edmx File and inserted Identity tables in it. Then I moved my T4 template to Core.Domain, here I created Partial Class for my AppUser and other Identity tables.
// This make my Core.Domain project dependent on `Microsoft.AspNet.Identity`
// Which is a violation.
public partial class AppUser : IdentityUser
{
//My Custom Properties
}
Now I have confusion about my AppUser Class which is in Core. According to the Onion Architecture standards, This whole layer should not a be dependent on any external library. But here I am using 'Microsoft.AspNet.Identity' in order to make my user as Identity User. Is there any solution for that?
The problem with AppUser deriving from IdentityUser is now your core project is dependant on a framework which it designed for Asp.Net, there your core project can become biased. You are correct that the core (i.e. central) project should be platform independent.
If you want to use IdentityUser you could define a User entity within your WebClient/WebApi projects which inherit from IdentityUser and keep them within these presentation layers, away from the core.
To convey your presentation user entity to the core you can manually map the properties to your core AppUser or use a mapping tool like AutoMapper
Edit
Included Diagram:
1-Core
- Domain Classes
-- AppUser
- Repository Interfaces
- Service Interfaces
2-Infrastructure
- Data //It contains my edmx file, I am using Db First approach.
- Dependency Injection
- Repository Interfaces Implementation
- Service Interfaces Implementation
3-WebApi
- Web Api Project
- Models
ApiUser : *(Inherits Api Identity framework)*
4-WebClient
- My AngularJs App
- Models
WebUser : *(Inherits IdentityUser)*
5-Test
- Test Project

Why does DataContext class create a new database?

Guys I am a new bee to asp .net mvc 3.
In the MS Visual Web Developer Express 2010, I created a new project using Visual C# / Web / ASP .net MVC 3 Web App using a form authentication template. I deleted all the controller and view files and added my own. I added a new sample.mdf database in the App_Data folder and changed the connection string accordingly and created country model.
But when I create CRUD controller for the country model it asks me for the DataContext class. It does not accept empty and I have to mention DataContext class. And when I create a DataContext class, it creates a new database of same name as the DataContext class. It does not use the database that I specified in the web.config file.
I want Country model data to be added in the sample.mdf file and not in the context database.
Please help... Thank you.
You can pass in the connection string to use in context constructor. That way you can use any database you wish.
A Data context class is simply just a class that maps your model classes to tables in the database. In the web config the connection string name should be the same as your context class (datacontext.cs):
<add name="datacontext" connectionString="[connectionstringhere]" />
If the connection string pointed to a SQL CE database that doesn’t exist... When you run the application, EF detects that the database didn’t exist and automatically creates it from the models set in the data context class.

Resources