如何做一个新的ASP.NET MVC应用5知道如何创建一个数据库和如何账户控制器访问数据库? [英] How does a new ASP.NET MVC 5 application know how to create a database and how does the Account Controller access the database?

查看:241
本文介绍了如何做一个新的ASP.NET MVC应用5知道如何创建一个数据库和如何账户控制器访问数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用我创建了一个 ASP.NET MVC 5 应用程序 的Visual Studio 2013更新2 。在应用方面,我有一个帐户控制器。我所用,并没有它的不同
包含的一个实例 的DbContext

I created an ASP.NET MVC 5 Application using Visual Studio 2013 Update 2. In the application, I have an Account controller. It's different from what I am used to and does not contain an instantiation of dbcontext.

public class AccountController : Controller
{
    private ApplicationUserManager _userManager;

    public AccountController()
    {
    }

    public AccountController(ApplicationUserManager userManager)
    {
        UserManager = userManager;
    }

    public ApplicationUserManager UserManager {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

我的的web.config 在缺省情况下创造了一个连接字符串是这样的:

My web.config that is created by default has a connection string like this:

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication3-20140417072624.mdf;Initial Catalog=aspnet-WebApplication3-20140417072624;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

能向我解释的人应用程序如何知道为这个应用程序创建一个数据库时,它开始的第一次?

Can someone explain to me how the application knows to create a database for this application when it starts for the first time?

此外,在随后的启动,它使用实体框架来访问Identity表做验证?

Also, on subsequent starts, does it use Entity Framework to access the Identity tables to do the authentication?

推荐答案

1)什么是怎么回事:

当你创建一个新的 MVC 5 应用程序,然后选择的单个用户帐户的,一个新的 ASP。 NET身份提供商包括它使用的实体框架6 code-首先

When you create a new MVC 5 application and choose "Individual User Accounts", a new ASP.NET Identity Provider is included which uses Entity Framework 6 Code-First.

微软已经通过的 EF-code-首先的制作的身份的可定制成为可能。

Microsoft has adopted EF-Code-First to make Identity as customizable as possible.

当身份被首次访问, 实体框架检查是否存在数据库。除非另有配置,它采用了DefaultConnection找到身份数据库。如果数据库不存在时,身份被调用时,的 EF 的自动创建的数据库。

When Identity is accessed for the first time, Entity Framework checks to see if the database exists. Unless configured otherwise, it uses the "DefaultConnection" to find the identity database. If the database does not exist when Identity is called, EF automatically created the database.

请注意您的连接字符串包含

Notice your connection string contains

`AttachDbFilename=|DataDirectory|\aspnet-WebApplication3-20140417072624.mdf`

如果您打开您的的App_Data 文件夹,你应该有一个的 ASPNET-WebApplication3-20140417072624.mdf 的文件。

If you open your App_Data Folder, you should have a aspnet-WebApplication3-20140417072624.mdf file.

如果您在本密度纤维板双击文件,在 VS2013服务器资源管理器会打开你的数据库。如果你已经尝试访问任何标识的功能,你将这些表创建的:

If you double click on this .mdf file, the VS2013 Server Explorer will open your DB. If you have already attempted to access any Identity functionality, you will these tables created:


  • _MigrationHistory

  • ASPNetRoles

  • ASPNetUserClaims

  • ASPNetUserLogins

  • ASPNetUsers

在默认情况下,您的应用程序被配置为使用的SQL Server Compact (MDF文件),所以你不必有一个实际的 SQL服务器实例上运行。所有这一切都是定制的。您的MDF文件的名称,标识数据库的模式, SQL精简 VS实际的 SQL服务器的实例。更改连接字符串,或创建一个新的,并传递给你的背景下,这个新的连接。

By default, your app is configured to use SQL Server Compact (MDF file) so you don't have to have an actual SQL Server Instance running. All of this is customizable. The name of your MDF file, the schema of Identity Database, the choice of SQL Compact vs an actual SQL Server instance. Change your Connection String, or create a new one and pass this new connection to your context.

2)哪里是我的背景?

这一切都很好,但你问了一个很重要的问题基本上是我的背景?,而正如有关隐含的问题,关于你如何可以进一步自定义您的数据库或改变验证逻辑。

All this is well and good, but an important question you asked is basically "Where is my context?", and the just as relevant implied questions regarding how you can further customize your DB or alter validation logic.

您会发现,你的项目引用 Microsoft.AspNet.Identity.EntityFramework 。该组件是一个执行 IdentityDBContext&LT; TUSER方式&gt; 和implentation 的UserManager

You will notice that your project references Microsoft.AspNet.Identity.EntityFramework. This assembly is an implementation of IdentityDBContext<TUser> and implentation of UserManager Class.

打开您的的AccountController ,并注意构造函数传递的UserManager 对象反过来有一个新UserStore 传入的对象,这将会通过了 ApplicationDbContext

Open your AccountController, and notice the constructor has UserManager object passed which in turn has a new UserStore object passed, which gets passed a ApplicationDbContext.

    public AccountController()
        : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))

ApplicationDbContext 模式文件夹的定义。这个文件夹里,你会发现一个 IdentityModels.cs 文件。打开它,你会看到

The ApplicationDbContext is defined in your Models Folder. Inside that folder, you will find an IdentityModels.cs file. Open it and you will see

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}

这是你的标识上下文分配。您可以更改传递给 ApplicationDbContext 构造连接的名称,或定义并在您的帐户控制器使用不同的背景。

This is where your Identity Context is assigned. you can change the connection name passed to the ApplicationDbContext constructor, or define and use a different context in your account controller.

3)如何自定义我的身份SCHEMA?

IdentityModels.cs 文件是继承的 ApplicationUser IdentityUser 类。

public class ApplicationUser : IdentityUser
{
}

您加入到这个类的任何属性将在您的 ASPNetUsers 表格被持久化。架构的其余部分在 IdentityDbContext定义类。所以,虽然你可以通过添加DBSet的上下文定义添加更多的表(例如特权)你的身份模式,

Any properties you add to this class will be persisted in your ASPNetUsers Table. The rest of the schema is defined in IdentityDbContext class. So, while you can add more tables (e.g. Privileges) to your Identity Schema by adding a DBSet to the Context Definition,

public DBSet<Privileges> { get; set; }

变更其他表(角色,权利要求等)也是可能的,但更复杂。例如,自定义的角色表格,你就必须实现从 IdentityRole NewIdentityRole 继承C>并添加它通过覆盖 OnModelCreating()法上下文关系。

Altering other tables (Roles, Claims, etc) is also possible, but far more involved. For example, to customize the Roles table, you would have to implement a NewIdentityRole inheriting from IdentityRole and add it's relationship by overriding the OnModelCreating() method for your Context.

此文章 <一个href=\"http://typecastexception.com/post/2014/02/13/ASPNET-MVC-5-Identity-Extending-and-Modifying-Roles.aspx\">Customizing角色表 确实描述涉及的步骤的一个好工作。即使在这里,你会发现,有投入到简单的添加新列显著的麻烦。删除从 IdentityDbContext创建的原始架构表或列级大概是尽可能多的麻烦,创建自己的实现 IdentityDbContext 类。

This article on Customizing Roles Tables does a good job of describing the steps involved. Even here, you will find that there is significant trouble invested into simply ADDING new columns. Removing tables or columns from the original schema created in the IdentityDbContext class is probably as much trouble as creating your own implementation of IdentityDbContext class.

这篇关于如何做一个新的ASP.NET MVC应用5知道如何创建一个数据库和如何账户控制器访问数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆