EF6 default role provider "method not found"

.net-4.5 asp.net-mvc-5 c# entity-framework-6

Question

I am using EF6 to build and MVC5 web application and i want to use the defualtRoleProvider and defaultMemberShip provider but i got this error

System.MissingMethodException: Method not found: 'System.Data.Objects.ObjectContext System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()'.

in the web.config i put this code

<membership defaultProvider="DefaultMembershipProvider">
  <providers>
    <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
  </providers>
</membership>
<roleManager enabled="true" defaultProvider="DefaultRoleProvider">
  <providers>
    <add connectionStringName="DefaultConnection" applicationName="/" name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers"/>
  </providers>
</roleManager>

and when i called this method i got the error

if (Roles.RoleExists("CustomerService") == false)
{
    Roles.CreateRole("CustomerService");
}

I am using code first with existing database,

Any help please?

Popular Answer

I had a similar issue a few months ago. Starting with (at least) the version of ASP.NET's default membership that rolled out with Visual Studio 2012, membership uses Entity Framework to do it's database interactions. The version that rolled out with VS2012 is tied to an earlier version of EF and is incompatible with EF6.

The solution is simple, if somewhat obscure. The form of membership rolled out with VS 2012 was also rolled out to NuGet as Microsoft ASP.NET Universal Providers Core (version 1). They released a new version (version 2) that is compatible with EF6. Go to NuGet, download Microsoft ASP.NET Universal Providers Core. Then make sure you update your web.config to reference the appropriate version of the providers library (currently 2.0.0).

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

If you're using Web Forms, also update your session provider.

<sessionState mode="InProc" customProvider="DefaultSessionProvider">
  <providers>
    <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="FirstChoice" />
  </providers>
</sessionState>



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why