本文介绍了替换自动创建的ApplicationDbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该以免责声明开头,尽管我花了最后一小时阅读文档,但我还是ASP.NET开发的新手,并且并不真正了解数据库上下文.当我构建ASP.NET MVC 5应用程序时,我选择了个人用户帐户身份验证. Visual Studio创建了一个名为IdentityModels.cs的文件,并在其中定义了ApplicationUser类和ApplicationDbContext类.

I should preface this with a disclaimer saying I'm new to ASP.NET development and don't really understand database contexts, despite spending the last hour reading documentation. When I built my ASP.NET MVC 5 application I chose to have individual user account authentication. Visual Studio created a file called IdentityModels.cs, and within there it defined an ApplicationUser class and a ApplicationDbContext class.

我已经完成了一些开发工作,并且我的CRUD控制器使用ApplicationDbContext与数据库进行通信,方法是在每个控制器上都具有以下私有属性:

I've done some development work, and my CRUD controllers use ApplicationDbContext to talk to the database, by having this private property on every controller:

private ApplicationDbContext db = new ApplicationDbContext();

然后,在控制器操作中,我将执行以下操作:

In the controller actions I then do things like:

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

我想整理这个数据库上下文,但是我需要首先了解它.我的主要问题是:

I want to tidy this database context stuff up, but I need to understand it first. My main questions are:

  1. 像我现在所做的那样,在整个应用程序中仅使用一个数据库上下文可以吗?
  2. 我可以用自己的替换IdentityModels.cs中定义的ApplicationDbContext类吗?
  3. ApplicationDbContext类是从IdentityDbContext<ApplicationUser>派生的,这是否意味着我应该为Visual Studio提供的用户身份验证内容和我自己的代码使用单独的数据库上下文?
  1. Is it okay to use just one database context for my entire application, like I'm doing now?
  2. Can I replace the ApplicationDbContext class defined in IdentityModels.cs with my own?
  3. The ApplicationDbContext class derives from IdentityDbContext<ApplicationUser>, does that mean I should have seperate database contexts for my user authentication stuff provided by Visual Studio, and my own code?

我认为我的最终目标是使用自己的数据库上下文,称为DatabaseContext,然后将其用于所有控制器都继承自的基本控制器中.然后,我只有一个实例化数据库上下文的位置,而不是在每个控制器中.

I think my end goal is to use my own database context, called DatabaseContext, which is then used in a base controller that all of my controllers inherit from. Then I only have one place where the database context is instantiated, instead of within every controller.

谁知道,我可能在想这件事的错误方法.每个人似乎都有自己喜欢的应对方式.

Who knows, I may be thinking the wrong way about this. Everybody seems to have their own preferred way of dealing with this.

谢谢!

推荐答案

  • 如果您决定直接从UI层访问DB(这是一个单独的讨论),那是可以的,因为ApplicationDbContext是控制器的私有字段,并且控制器根据请求创建和处置-ApplicationDbContext将根据请求创建并处理.
    • If you decided that you are going to have access to DB directly from UI layer (Which is a separate discussion) than it is OK, since ApplicationDbContext is a private field of your controller and controllers are created and disposed per request - ApplicationDbContext will be created and disposed per request.
      • 您绝对可以做到的.它用于创建一个UserStore,该UserStore接收DbContext作为构造函数中的参数,因此此
        • You definitely can do it. It is used to create a UserStore which receives DbContext as an argument in constructor so this
        • var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(new CustomDbContext("connection")));

          将起作用.您仍然必须确保ApplicationUser是您的自定义上下文中的实体.当然,您也可以覆盖/替换ApplicationUser.

          will work. You still have to make sure that ApplicationUser is an entity in your custom context. Of course you can override/replace ApplicationUser as well.

          默认情况下,Asp.Net Identity会为您生成一个新数据库,并且ApplicationDbContext配置为可与此数据库一起使用.您也可以将与身份验证相关的实体存储在任何其他数据库中,只需确保所有相关表都在其中.您还可以扩展此数据库以包括您在应用程序中使用的其他表,这样您就可以始终使用相同的上下文.

          By default Asp.Net Identity generates a new data base for you and ApplicationDbContext configured to work with this data base. You can store your authentication related entities in any other data base as well, you just need to make sure that all the related tables are there.You can also extend this data base to include other tables that you are using in your application so you could use the same context all over.

          PS: ApplicationDbContext不必实现IdentityDbContext<ApplicationUser>,扩展默认的DbContext也可以(如果您已经生成了Db,则必须对其进行更新\使用代码)迁移以使以下各项正常工作):

          P.S: ApplicationDbContext doesn't have to implement IdentityDbContext<ApplicationUser>, Extending a default DbContext works as well (if you already generated a Db you will have to update it\use code migrations for the following to work):

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

          这篇关于替换自动创建的ApplicationDbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:28