3

したがって、Entity Framework 5 を使用した基本的な MVC 4 インターネット アプリケーション プロジェクトがあります。

ユーザー用のテーブルを使用する 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() 呼び出しを削除しましたが、ロールが既に存在するため、データベースの更新は失敗します。

ここで何が欠けていますか?このロールはどこに保存されますか?

4

2 に答える 2

4

I came across into an error The Role Manager feature has not been enabled mentioned by @Magnus. Hope this probably related to your roles issue in which the default role provider is unknown.

The error The Role Manager feature has not been enabled happened when I move the WebSecurity.InitializeDatabaseConnection method from MVC project into a class library (data access) for code first migration seed.

The point is App.config need to be configured for Package Manager Update-Database command to be run just like when Web.config exist.

Below is my App.config and Seed. Note that

  1. I add "DefaultConnection" connection string *
  2. Add system.web->roleManager to set the enable="true"
  3. Add runtime->assemblyBinding->qualifyAssembly to give hint to compiler where is WebMatrix.WebData by giving the full assembly name.

App.config:

<?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>

Seed Method:

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");
        }
    }
}

Hope this will help anyone with The Role Manager feature has not been enabled issue when using code first migration seed in a class library.

Update

* EF5 will read the connection string in Web.config of Mvc project but not in the App.config of the project with EF Migrations. However the membership and roleManager settings is still required in the EF Migration project.

于 2013-01-07T22:16:37.123 に答える