Search code examples
c#asp.net-mvcsimplemembership

Unable to find .net data provider exception when implementing SimpleMembership


I am a newbie. I am using SimpleMembership to generate the Register and login tables. However, I am getting the following exception.

EXCEPTION

An exception of type 'System.ArgumentException' occurred in System.Data.dll but was not handled in user code

Additional information: Unable to find the requested .Net Framework Data Provider. It may not be installed.

enter image description here

CODE

    [AllowAnonymous]
    [HttpGet]
    public ActionResult Register1()
    {
        if (!WebSecurity.Initialized)
        {


            //WebSecurity.InitializeDatabaseConnection("My_Entities", "Users", "Id", "UserName", autoCreateTables: true);
            WebSecurity.InitializeDatabaseConnection("My_Entities", "User", "Id", "Email", autoCreateTables: true);
        }
        return View("Register");
    }
    [AllowAnonymous]
    [HttpPost]
    public ActionResult Register1(FormCollection form)
    {
        WebSecurity.CreateUserAndAccount(form["username"], form["password"], new { DisplayName = form["displayname"], Country = form["country"] });
        Response.Redirect("~/account/login");
        return View();
    }

WEB.CONFIG

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <!-- 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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <membership defaultProvider="p1">
      <providers>
        <add name="p1" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
      </providers>
    </membership>
    <roleManager enabled="true">
      <providers>
        <add name="p1" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
      </providers>
    </roleManager>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <connectionStrings>
    <add name="MY_Entities" connectionString="metadata=res://*/Models.DB.csdl|res://*/Models.DB.ssdl|res://*/Models.DB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MYCOMP\MYCOM;initial catalog=MY;persist security info=True;user id=sa;password=1*34;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="System.Data.SqlServerCe.4.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
    </providers>
  </entityFramework>
</configuration>

Solution

  • The provided web.config file gives presence of System.Data.SqlServerCe.4.0 as Entity Framework's data provider on this part:

    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
            <parameters>
                <parameter value="System.Data.SqlServerCe.4.0" />
            </parameters>
        </defaultConnectionFactory>
        <providers>
            <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
        </providers>
    </entityFramework>
    

    Since the error clearly said that the requested data provider is not yet installed, find out machine.config file in %Windows%\Microsoft.NET\Framework\[version number]\Config, edit it in admin privileges then add these lines below (or if you're being lazy to dig up machine.config, place them into web.config under system.data element):

    <system.data>
        <DbProviderFactories>
            <remove invariant="System.Data.SqlServerCe.4.0" />
            <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
        </DbProviderFactories>
    </system.data>
    

    Also, remove any single-terminating element of <DbProviderFactories /> if you found them.

    Next, you may adding references related to SQL Server CE (sqlce*.dll) using Package Manager Console by finding EntityFramework.SqlServerCompact package (refer this one) and install it on your solution. Additionally, change provider name System.Data.SqlClient to System.Data.SqlServerCe.4.0 in EF (and SQL Server) connection string when required.

    References:

    Unable to find the requested .Net Framework Data Provider

    Unable to find the requested .Net Framework Data Provider. It may not be installed.

    Unable to find the requested .Net Framework Data Provider. It may not be installed. - when following mvc3 asp.net tutorial