- 用户信息表UserProfile
- 角色表webpages_Roles
- webpages_Membership(用于存储Membership的信息内容)
- 权限表webpages_Permission
- 关系表webpages_PermissionsInRoles(用于存储角色的具体权限)
2. 如果使用 Machine\SQLExpress,请确定Connection String in the web.config
在Web.Config中,在其“System.Web” 部分中加入:
1 <roleManager enabled="true" defaultProvider="SimpleRoleProvider">
2 <providers>
3 <clear/>
4 <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
5 </providers>
6 </roleManager>
7 <membership defaultProvider="SimpleMembershipProvider">
8 <providers>
9 <clear/>
10 <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
11 </providers>
12 </membership>
13
4. 在PM> enable-migrations2 <providers>
3 <clear/>
4 <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
5 </providers>
6 </roleManager>
7 <membership defaultProvider="SimpleMembershipProvider">
8 <providers>
9 <clear/>
10 <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
11 </providers>
12 </membership>
13
不要忘记在model中加入我们将要添加了元素譬如这里的Mobile,Company和Position
5. 会发现我们有Configuration.cs在Migrations文件夹中
设置AutomaticMigrationsEnabled = true;
加入,譬如:
1 protected override void Seed(TestProject.Models.UsersContext context)
2 {
3 WebSecurity.InitializeDatabaseConnection(
4 "DefaultConnection",
5 "UserProfile",
6 "UserId",
7 "UserName", autoCreateTables: true);
8
9 if (!Roles.RoleExists("Administrator"))
10 Roles.CreateRole("Administrator");
11
12 if (!WebSecurity.UserExists("alex"))
13 WebSecurity.CreateUserAndAccount(
14 "uername",
15 "password",
16 new { Email = "username@gmail.com", Mobile = "+176", Company = "Company_A", Position = "0" }
17 );
18
19 if (!Roles.GetRolesForUser("alex").Contains("Administrator"))
20 Roles.AddUsersToRoles(new[] { "alex" }, new[] { "Administrator" });
21 }
22
6. 在PM> update-database -verbose2 {
3 WebSecurity.InitializeDatabaseConnection(
4 "DefaultConnection",
5 "UserProfile",
6 "UserId",
7 "UserName", autoCreateTables: true);
8
9 if (!Roles.RoleExists("Administrator"))
10 Roles.CreateRole("Administrator");
11
12 if (!WebSecurity.UserExists("alex"))
13 WebSecurity.CreateUserAndAccount(
14 "uername",
15 "password",
16 new { Email = "username@gmail.com", Mobile = "+176", Company = "Company_A", Position = "0" }
17 );
18
19 if (!Roles.GetRolesForUser("alex").Contains("Administrator"))
20 Roles.AddUsersToRoles(new[] { "alex" }, new[] { "Administrator" });
21 }
22
7. Server Explorer → Data Connections,可以找到Tables,并可以在 SQL 中运行 update query 用于加入columns
8. PM> update-database -verbose
这就是整个migrations的过程。
具体使用请参考:
http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/
没有评论:
发表评论