7

I am trying to set up a simple ASP.NET MVC 4 webapp using DB first migrations from a SQL Server (2005). I have created the tables in the database and used Entity Framework to create the objects in the code. I can access the data using these objects.

The problems come when I try to initialize the WebSecurity using WebSecurity.InitializeDatabaseConnection("FLMREntities", "UserProfile", "UserId", "UserName", true); in the Global.asax.cs file. I have tried using the InitializeSimpleMembershipAttribute filter that came with the template and got the same issue. I get the error message:

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

Here is the relevant connection string:

<add name="FLMREntities"
     connectionString="metadata=res://*/Models.FLMR.csdl|res://*/Models.FLMR.ssdl|res://*/Models.FLMR.msl;
                    provider=System.Data.SqlClient;
                    provider connection string=&quot;data source=notes.marietta.edu;
                        initial catalog=muskwater;
                        user id=muskwater;password=********;
                        MultipleActiveResultSets=True;
                        App=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />

Also, I have created the membership tables in the database to match what the template creates. If I change the final parameter in the Initialize call to false (so that it does not try to create the tables automatically) it returns that it cannot find the UserProfile table. I have also tried variations on the names, such as [dbo].[UserProfile].

All I need is to have a simple account model to allow users to log in and allow certain users to see more content.

amoscardino
  • 71
  • 3
  • 5
  • Do you have a `` before your connection string(s)? To prevent you inheriting any from other configs – Basic Mar 04 '13 at 16:53
  • I have no other connection strings in this project. I commented out the one from the template. Would I still need the `` and would that go on the same nested level as the one above? – amoscardino Mar 04 '13 at 19:34
  • It's possible for either machine-/site-wide config to add connection strings to your application. Some frameworks do too. The `` should be the first element in the same section as your connection string (order is preserved when config sections are loaded). – Basic Mar 04 '13 at 21:56

3 Answers3

4

I had a similar problem, what I've done: I modified the default connection. I already had a connection string for the edmx model, which looked like this:

<add name="ChemicalReporterEntities" connectionString="metadata=res://*/ChemicalDB.csdl|res://*/ChemicalDB.ssdl|res://*/ChemicalDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLExpress;initial catalog=ChemicalReporter;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

But I cannot used it with the SimpleMemebrship provider. So in Visual Studio I opened the Server Explorer > Data Connections, I selected my connection, right click, properties and I copied the connection string from there and pasted it to defaultConnection:

<add name="DefaultConnection" connectionString="Data Source=.\SQLExpress;Initial Catalog=ChemicalReporter;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework" providerName="System.Data.SqlClient" />

After that I changed WebSecurity.InitializeDatabaseConnection to use the DefaultConnection.

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", true);
czadam
  • 1,827
  • 1
  • 18
  • 31
1

This could be a duplicate of this.

From what I remember about this problem, you need to make sure that the sqldata provider is registered. Sometimes, in your machine.config there is a duplicate entry that you need to delete and if I remember correctly, there could be an erroneous entry that you need to remove. Have a look at this msdn post, the info for this is about halfway down the page.

The link to the other SO has some links that could be helpful. If this ends up not being marked as a duplicate question, I can add them here as well or whatever is the proper thing to do.

The section you are looking for will look similar to this I think:

<system.data>
  <DbProviderFactories>
    <add name="SqlClient Data Provider"
     invariant="System.Data.SqlClient" 
     description=".Net Framework Data Provider for SqlServer" 
     type="System.Data.SqlClient.SqlClientFactory, System.Data, 
     Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    />
  </DbProviderFactories>
</system.data>

From the information you have provided in comments, it seems that this is missing in your machine.config. Unless I am mistaken (possible) that means that the SQL Provider isn't registered on your machine. You'll need to add the above section to your web.config. You'll also need to make sure that System.Data.SqlClient is referenced in your project.

If this isn't installed on your target machine, you'll have more work to do, getting it installed. I wish I could help you with that more, but when I did this, it was for SqlServerCE, so the files I used are much different.

Good luck!

EDIT: A critical piece of information, is the version needs to match the file that you are using. in type="blabh..blah..blah..Version=2.0.0.0, blah blah" that needs to be the version of the file you are using so right click your file and get the file version. If it fails, try variations of the version. I believe my file was 2.0.0.1 but Version=2.0 is what worked when I did it. Just try a few different versions based on your version number (2.0, 2.0.0, 2.0.0.1 for example).

Community
  • 1
  • 1
Mike C.
  • 3,024
  • 2
  • 21
  • 18
  • The answer in that question really doesn't explain how to fix the problem. I have located the machine.config file and there are only DbProviderFactory entries for SqlServerCe. Do I need to add an entry for SqlClient? Will it just look like the one you posted? – amoscardino Mar 04 '13 at 19:40
  • I wouldn't modify the machine.config unless you know what you are doing. Let me see if I can find the resources that I used when I had this problem so that you have a better idea. – Mike C. Mar 04 '13 at 19:47
  • Also, FYI, when I had the exact same error as you (it was at runtime), this was the solution. I had to make sure to deploy the correct files and I had to add the section I have listed to my web.config. – Mike C. Mar 04 '13 at 19:58
  • 1
    I managed to solve it using [this article](http://dotnetnsqlcorner.blogspot.in/2012/04/membership-in-aspnet-mvc-4.html), mainly by installing System.Web.Providers. – amoscardino Mar 05 '13 at 20:47
0

Just wanted to chime in here, that I had this issue, by looking at my machine.config for the targeted .NET version that I was running off of... within the structure:

ensure that your targeted data providers are correctly inputted within there, only one per provider (I have seen duplicates cause problems), as well as, ensure that it is valid XML, and you only have one DBProviderFactories entry. In my case, I had a entry after the initial entry (essentially two entries, one with my providers, one blank), and the blank was was causing all the issues.

Hope this helps someone with the same issue :)

Sean Haddy
  • 1,630
  • 1
  • 16
  • 25