Replacing an automatically created ApplicationDbContext

I have to preface this with a disclaimer stating that I am new to ASP.NET development and do not understand the contexts of the database, despite having spent the last reading of the documentation. When I built the ASP.NET MVC 5 application, I chose to individually authenticate the user account. Visual Studio created a file called IdentityModels.cs , and there he defined the ApplicationUser class and ApplicationDbContext .

I did some development work, and my CRUD controllers use ApplicationDbContext to communicate with the database, having this private property on each controller:

 private ApplicationDbContext db = new ApplicationDbContext(); 

In the actions of the controller, I do things like:

 return View(db.Trains.ToList()); 

I want to remove this database context, but first I have to figure it out. My main questions are:

  • Can I use only one database context for my entire application, as of now?
  • Can I replace the ApplicationDbContext class defined in IdentityModels.cs with my own?
  • The ApplicationDbContext class is derived from IdentityDbContext<ApplicationUser> , does this mean that I have to have separate database contexts for user authentication files provided by Visual Studio, and my own code?

I think my ultimate goal is to use my own database context, called DatabaseContext , which is then used in the base controller, which all my controllers inherit from. Then I have only one place where the database context is created, and not inside each controller.

Who knows, I could be wrong about that. Everyone seems to have their own preferred way of dealing with this.

Thanks!

+6
source share
2 answers

Is it possible to use only one database context for my entire application, for example, what am I doing now?

  • If you decide that you will have access to the database directly from the user interface level (which is a separate discussion), than this is normal, since ApplicationDbContext is a private field of your controller and the controllers are created and deleted per request - ApplicationDbContext will be created and deleted by request .

Can I replace the ApplicationDbContext class defined in IdentityModels.cs with my own?

  • You can definitely do this. It is used to create a UserStore that receives a DbContext as an argument in the constructor, so this

var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(new CustomDbContext("connection")));

will work. You still need to make sure that ApplicationUser is an entity in your user context. Of course, you can override and replace ApplicationUser .

The ApplicationDbContext class comes from IdentityDbContext, does this mean that I need separate database contexts for my Visual Studio user authentication files and my own code?

By default, Asp.Net Identity creates a new database for you and ApplicationDbContext configured to work with this database. You can also store authentication related objects in any other database, you just need to make sure that all related tables are there. You can also expand this database to include other tables that you use in your application so that you can use the same context.

PS: ApplicationDbContext does not need to implement IdentityDbContext<ApplicationUser> , the default DbContext also works (if you have already generated Db, you will have to update it \ use the migration code for the following actions):

  public class ApplicationDbContext : DbContext { public ApplicationDbContext() : base("DefaultConnection") { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } } 
+8
source

I think you would like to create your own authentication database for login authentication. You must follow these instructions step by step. 1) you need to create your own class to implement user authentication. for example, I am creating the next student class. You can create this class in the MODEL section of MVC.

 class student{ public int rollno{get;set;} public string firstName{get;set;} public string lastName{get;set;} 

2) now you will create your own dbcontext using identityDbContext for the student class. You can create this class in the App_Start section like this.

 public class ApplicationDbContext : IdentityDbContext<student> { public ApplicationDbContext() : base() { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } 

3) now you can use it at home in the controller, following

 public class HomeController : Controller { // GET: Home public async Task<ActionResult> Index() { var context = new ApplicationDbContext(); // DefaultConnection var store = new UserStore<student>(context); var manager = new UserManager<student>(store); var signInManager = new SignInManager<student, string>(manager, HttpContext.GetOwinContext().Authentication); var username = "Edward"; var email = " abc@gmail.com "; var password = "Your password"; var user = await manager.FindByEmailAsync(email); if (user == null) { user = new student { UserName = username,//username ,Email are getting from identityDbContext and the other three RollNO,FirstName and LastName are your own Custom members.so like this you can extend your any kind of data you like add or extend with IdentityDbContext. Email = email, RollNo = "15", FirstName = "David", LastName = "Kandel" }; await manager.CreateAsync(user, password); } else { var result = await signInManager.PasswordSignInAsync(user.UserName, password, true, false); if (result == SignInStatus.Success) { return Content("Hello, " +user.rollno + "" + user.FirstName + " " + user.LastName); } } return Content("Hello, Index"); } } 

+1
source

Source: https://habr.com/ru/post/981177/


All Articles