我遇到了The Role Manager feature has not been enabled
@Magnus 提到的一个错误。希望这可能与您的角色问题有关,其中默认角色提供者未知。
The Role Manager feature has not been enabled
当我将WebSecurity.InitializeDatabaseConnection
方法从 MVC 项目移动到类库(数据访问)以进行代码优先迁移种子时,发生了错误。
关键是App.config
需要配置包管理器Update-Database
命令才能像Web.config
存在时一样运行。
下面是我的App.config
和Seed
。注意
我添加"DefaultConnection"
连接字符串*
- 添加
system.web->roleManager
以设置enable="true"
- 添加以通过提供完整的程序集名称
runtime->assemblyBinding->qualifyAssembly
来提示编译器在哪里。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
and设置。roleManager