asp.net-mvc 如何使用实体框架和成员表初始化数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12662587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to initialize database with Entity Framework and Membership tables
提问by Patrick Desjardins
I have a MVC4 web application that use Entity Framework 5.0 Code First.
我有一个使用 Entity Framework 5.0 Code First 的 MVC4 Web 应用程序。
In Global.asax.cs I have a bootstrapper that initialize the Entity.Database, force the database to be initialized and initialize the database for the Membership. The code is this one:
在 Global.asax.cs 中,我有一个初始化 Entity.Database 的引导程序,强制初始化数据库并为成员资格初始化数据库。代码是这样的:
System.Data.Entity.Database.SetInitializer(new DatabaseContextInitializer());
Database.Initialize(true);
WebSecurity.InitializeDatabaseConnection(DEFAULTCONNECTION, "UserProfile", "UserId", "UserName", autoCreateTables: true);
The DatabaseContextInitializer is very simple for the moment:
DatabaseContextInitializer 目前非常简单:
public class DatabaseContextInitializer : DropCreateDatabaseIfModelChanges<DatabaseContext>
{
protected override void Seed(DatabaseContext dbContext)
{
base.Seed(dbContext);
db.Set<Workout>().Add(new Workout {Id = 1, Name = "My First workout user1"})
}
}
The problem is that I cannot create User to the membership with:
问题是我无法为会员创建用户:
WebSecurity.InitializeDatabaseConnection(DEFAULTCONNECTION, "UserProfile", "UserId", "UserName", autoCreateTables: true);
Because I have a problem with that the database is not created. How do you initialize some default user for your database with Entity Framework 5.0 and Asp.Net MVC 4?
因为我有一个问题,即未创建数据库。您如何使用 Entity Framework 5.0 和 Asp.Net MVC 4 为您的数据库初始化一些默认用户?
回答by Darin Dimitrov
Take a look at the following articlefor the recommended approach for seeding your database using migrations.
请查看以下文章,了解使用迁移为数据库设定种子的推荐方法。
Here are the steps:
以下是步骤:
- Create a new ASP.NET MVC 4 application using the Internet Template
In your package manager console type the following command:
enable-migrationsThis will create a
~/Migrations/Configuration.csfile in which you could seed your database:using System.Data.Entity.Migrations; using System.Linq; using System.Web.Security; using WebMatrix.WebData; internal sealed class Configuration : DbMigrationsConfiguration<MvcApplication1.Models.UsersContext> { public Configuration() { AutomaticMigrationsEnabled = true; } protected override void Seed(MvcApplication1.Models.UsersContext context) { WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); if (!Roles.RoleExists("Administrator")) { Roles.CreateRole("Administrator"); } if (!WebSecurity.UserExists("john")) { WebSecurity.CreateUserAndAccount("john", "secret"); } if (!Roles.GetRolesForUser("john").Contains("Administrator")) { Roles.AddUsersToRoles(new[] { "john" }, new[] { "Administrator" }); } } }Specify the memebership and role providers in your web.config:
<roleManager enabled="true" defaultProvider="SimpleRoleProvider"> <providers> <clear/> <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/> </providers> </roleManager> <membership defaultProvider="SimpleMembershipProvider"> <providers> <clear/> <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" /> </providers> </membership>Run the migration in your package manager console:
update-database -verbose
- 使用 Internet 模板创建一个新的 ASP.NET MVC 4 应用程序
在您的包管理器控制台中键入以下命令:
enable-migrations这将创建一个
~/Migrations/Configuration.cs文件,您可以在其中设置数据库的种子:using System.Data.Entity.Migrations; using System.Linq; using System.Web.Security; using WebMatrix.WebData; internal sealed class Configuration : DbMigrationsConfiguration<MvcApplication1.Models.UsersContext> { public Configuration() { AutomaticMigrationsEnabled = true; } protected override void Seed(MvcApplication1.Models.UsersContext context) { WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); if (!Roles.RoleExists("Administrator")) { Roles.CreateRole("Administrator"); } if (!WebSecurity.UserExists("john")) { WebSecurity.CreateUserAndAccount("john", "secret"); } if (!Roles.GetRolesForUser("john").Contains("Administrator")) { Roles.AddUsersToRoles(new[] { "john" }, new[] { "Administrator" }); } } }在 web.config 中指定成员资格和角色提供者:
<roleManager enabled="true" defaultProvider="SimpleRoleProvider"> <providers> <clear/> <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/> </providers> </roleManager> <membership defaultProvider="SimpleMembershipProvider"> <providers> <clear/> <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" /> </providers> </membership>在包管理器控制台中运行迁移:
update-database -verbose

