Question

I have an MVC4 application with the models and the migrations in a separate project, which is a class library.

I am trying to seed the database with old users using the following code inside my seed method

foreach (var subcontractor in context.Subcontractors)
{
    WebSecurity.CreateUserAndAccount(subcontractor.Email, subcontractor.ObsoletePlainTextPassword);
}

I copied the following code from my main project into the app.config, but I don't think it's getting picked up.

<profile defaultProvider="DefaultProfileProvider">
  <providers>
    <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
  </providers>
</profile>
<membership defaultProvider="DefaultMembershipProvider">
  <providers>
    <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>
<roleManager defaultProvider="DefaultRoleProvider">
  <providers>
    <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
  </providers>
</roleManager>

Where I run update-database, i get The Role Manager feature has not been enabled. error. What am I missing?

Was it helpful?

Solution

The problem was the fact that my "Data" project wasn't a StartUp project. App.config wasn't being read, and that was causing the error.

OTHER TIPS

MVC4 rebuilt (or more borrowed you can say) the authentication system from WebMatrix, so make sure you're not mixing any code from the prior auth system. But if you're using the default security and authentication (which is now SimpleMembership) in there and you're trying to seed roles, you need to enable the SimpleRoleProvider in the web.config. Are you trying to seed a user or users with corresponding roles in the seed method for Entity migrations? If you're not seeding the database with roles, then it might be something else. Test this by removing any mention of seeding roles in the migrations/congiguration.cs file. So be sure any mention of SimpleRoleProvider is out of the configuration.cs file, or try the following code in the 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>

The reason for this is while the SimpleRoleProviders is available as part of the MVC when the website is running, this role provider has to be explicitly configured as part of the seed method for migrations. Otherwise the seed method in migrations won't pick it up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top