初始化数据库实体框架6 code首先 [英] Initialize database with Entity Framework 6 Code First

查看:129
本文介绍了初始化数据库实体框架6 code首先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是这些线路的等效 EF5 SimpleMembershipProvider

What is the equivalent of these lines EF5 with SimpleMembershipProvider:

if (!WebMatrix.WebData.WebSecurity.Initialized)
{
    WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}

EF6 ASP.NET的身份

这些行来自Global.asax文件中的方法保护无效的Application_Start()

Those lines come from the global.asax file in the method protected void Application_Start().

推荐答案

实体框架实现ASP.NET身份使用code首先建立映射和初始化数据库。它只是使用标准的EF code首先初始化数据库,所以是不是真的被上面提到的相当于code(他们实际上是调用简单成员身份API,而不是EF)。

The Entity Framework implementation of ASP.NET Identity uses Code First to create mappings and initialize the database. It simply uses standard EF Code First to initialize the database, so there isn't really the equivalent code that was referenced above (they're actually calls to the Simple Membership API, not EF).

在该 ApplicationDbContext 首次访问(在默认MVC模板,这种情况发生在的AccountController ),EF运行环境的初始化。默认情况下,它使用 CreateDatabaseIfNotExists 初始化,检查,看看是否创建表,如果需要创建它们。

When the ApplicationDbContext is first accessed (in the default MVC template, this happens in the AccountController), EF runs the context's initializer. By default, it uses the CreateDatabaseIfNotExists initializer, which checks to see if the tables are created and creates them if needed.

简单调用成员指定要使用的连接字符串的名称。在ASP.NET的身份,这是坐落在 ApplicationDbContext 的构造函数(以 IdentityModels.cs )。

The Simple Membership call specifies the name of the connection string to use. In ASP.NET Identity, this is set in the constructor of ApplicationDbContext (in IdentityModels.cs).

简单会员 InitializeDatabaseConnection()方法也需要在表和列的名称参数。 IdentityDbContext (其中 ApplicationDbContext 从继承)包括一些默认的映射,映射到与ASPNET pfixed $ P $表。您应该能够通过重写改变他们 OnModelCreating() ApplicationDbContext 并提供自定义映射。

The Simple Membership InitializeDatabaseConnection() method also takes in parameters for the table and column names. IdentityDbContext (which ApplicationDbContext inherits from) includes some default mappings to map to tables that are prefixed with "AspNet". You should be able to change them by overriding OnModelCreating() in ApplicationDbContext and supplying custom mappings.

和,因为它只是EF code首先,你也可以创建自己的自定义数据库的初始化,您可以提供种子()方法,增加了一些初步的角色和用户,例如

And because it's just EF Code First, you can also create your own custom database initializer where you can supply a Seed() method that adds some initial roles and users, for example.

这篇关于初始化数据库实体框架6 code首先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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