所以我使用Entity Framework 5获得了基本的MVC 4 Internet应用程序项目。
我已经为用户配置了使用我的表格的WebSecurity。
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "Users", "Id", "Email", autoCreateTables: true);
然后在我的迁移配置类中,我使用新角色为DB添加种子并向其添加用户。
internal sealed class Configuration : DbMigrationsConfiguration<Infrastructure.KlepecV2Db>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(Infrastructure.KlepecV2Db context)
{
if(!Roles.RoleExists("Admin"))
{
Roles.CreateRole("Admin");
}
if (!Roles.RoleExists("Test1"))
{
Roles.CreateRole("Test1");
}
if(Membership.GetUser("user1") != null)
{
if(!Roles.IsUserInRole("user1","Admin"))
{
Roles.AddUserToRole("user1", "Admin");
}
}
if (Membership.GetUser("user2") != null)
{
if (!Roles.IsUserInRole("user2", "Admin"))
{
Roles.AddUserToRole("user2", "Admin");
}
}
}
}
因此,当我在NuGet控制台中键入“update-database”时,所有内容都会执行而不会出错。但是如果我把webpages_Roles表看成空了。 webpages_UsersInRoles也是空的。
为了测试我删除了Role.RoleExists()调用并更新数据库失败,因为角色已经存在。
我在这里缺少什么?这些角色存储在哪里?
答案 0 :(得分:4)
我遇到了@Magnus提到的错误The Role Manager feature has not been enabled
。希望这可能与您的角色问题有关,其中默认角色提供程序是未知的。
当我将<{1>}方法从MVC项目>>移动到类库(数据访问)以进行代码优先迁移时,发生错误The Role Manager feature has not been enabled
种子。
需要为包管理器WebSecurity.InitializeDatabaseConnection
命令配置App.config
,就像Update-Database
存在时一样。
以下是我的Web.config
和App.config
。注意
Seed
连接字符串"DefaultConnection"
以设置system.web->roleManager
enable="true"
以向编译器提供runtime->assemblyBinding->qualifyAssembly
的提示。App.config中:
WebMatrix.WebData
种子方法:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<!--<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Db;Persist Security Info=True;User=user;Password=pass" providerName="System.Data.SqlClient" />-->
</connectionStrings>
<system.web>
<roleManager enabled="true" defaultProvider="simple">
<providers>
<clear />
<add name="simple" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
</providers>
</roleManager>
<membership defaultProvider="simple">
<providers>
<clear />
<add name="simple" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
</providers>
</membership>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="WebMatrix.WebData" fullName="WebMatrix.WebData, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>
希望在类库中使用代码优先迁移种子时,这将有助于protected override void Seed(DatabaseContext context)
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "Users", "Id", "Email", autoCreateTables: true);
if(!Roles.RoleExists("Admin"))
{
Roles.CreateRole("Admin");
}
if (!Roles.RoleExists("Test1"))
{
Roles.CreateRole("Test1");
}
if(Membership.GetUser("user1") != null)
{
if(!Roles.IsUserInRole("user1","Admin"))
{
Roles.AddUserToRole("user1", "Admin");
}
}
if (Membership.GetUser("user2") != null)
{
if (!Roles.IsUserInRole("user2", "Admin"))
{
Roles.AddUserToRole("user2", "Admin");
}
}
}
问题的任何人。
The Role Manager feature has not been enabled
EF5将读取Mvc项目的Web.config中的连接字符串,但不会读取具有EF迁移的项目的App.config中的连接字符串。但是,EF迁移项目仍然需要*
和membership
设置。
答案 1 :(得分:0)
想把我的0.02美元加到谈话中。我有类似CallMeLaNN的设置,但仍然收到错误。我的解决方案是将我的“数据”项目设置为StartUp项目。之后,我的app.config文件开始被提起,我现在可以从Update-Database
命令中为我的用户播种。