问题描述
我必须使用实体框架5.0 code首先一个MVC4 Web应用程序。
I have a MVC4 web application that use Entity Framework 5.0 Code First.
在Global.asax.cs中我有一个初始化Entity.Database引导程序,强制进行初始化数据库,初始化数据库为会员。在code是这一个:
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:
System.Data.Entity.Database.SetInitializer(new DatabaseContextInitializer());
Database.Initialize(true);
WebSecurity.InitializeDatabaseConnection(DEFAULTCONNECTION, "UserProfile", "UserId", "UserName", autoCreateTables: true);
该DatabaseContextInitializer是当下非常简单:
The DatabaseContextInitializer is very simple for the moment:
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);
由于我有不创建数据库中的一个问题。你如何初始化一些默认的用户与实体框架5.0和Asp.Net MVC 4数据库?
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?
推荐答案
看看的<一个href=\"http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-$c$cfirst-and-custom-user-properties/\">following文章使用迁移播种数据库推荐的方法。
Take a look at the following article for the recommended approach for seeding your database using migrations.
下面的步骤是:
- 创建使用互联网的模板创建一个新的ASP.NET MVC应用程序4
-
在你的包管理控制台,键入以下命令:
- Create a new ASP.NET MVC 4 application using the Internet Template
In your package manager console type the following command:
enable-migrations
这将创建一个〜/迁移/ Configuration.cs
文件,在其中你可以播种数据库:
This will create a ~/Migrations/Configuration.cs
file 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" });
}
}
}
指定您的web.config中memebership和角色提供:
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
这篇关于如何与实体框架和会员表初始化数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!