SimpleMembershipInitializer won't initialize
Asked Answered
G

2

8

I am struggling with getting a simplemembership scenario working in my EntityFramework / MVC4 / DatabaseFirst project. I've found plenty of examples for working with code first, but nothing for DB first.

The problem I'm encountering is the the InitializeDatabaseConnection is throwing an error ("Unable to find the requested .Net Framework Data Provider. It may not be installed.") The code looks like this :

WebSecurity.InitializeDatabaseConnection("DALEntities", "tblContacts1", "ContactID", "EMail", autoCreateTables: true);

I am not sure what DataProvider is failing. If I try to trace 'into' the InitializeDatabaseConnection call, it immediately throws the error.

What am I missing?

Info:
DALEntities is the name of the connectionString that the rest of EF uses. The following code works just fine....

    public ActionResult Test() {
        using (var db = new DALEntities()) {
            var query = from i in db.TBLINVENTORies
                            orderby i.ITEMNAME
                            select i;
            var cnt = query.Count();
            string str = "Total Inventory: " + cnt;
            return Content(str);
        }
    }

My connection strings section from the web.config:

  <connectionStrings>
    <add name="DALEntities" connectionString="metadata=res://*/DAL.DAL.csdl|res://*/DAL.DAL.ssdl|res://*/DAL.DAL.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SOMECOMPUTER;initial catalog=SOMEDB;persist security info=True;user id=SOMEID;password=SOMEPASS;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

This post seems to be asking the same question (but in context of model-first), but there is no solution yet : Using SimpleMembership with EF model-first

Also, I see that there is an overload for WebSecurity.InitializeDatabaseConnection() with the help text: Initializes the membership system ((blah blah <snip> ProviderName: the name of the ADO.NET data provider. If you want to use Microsoft SQL Server, the WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection(String, String, String, String, Boolean) overload is recommended . I do wish to connect to a MSSQL server...will this be required?

Gout answered 15/10, 2012 at 19:30 Comment(5)
my projects didn't like using the EF connection string. instead I had to add a second connection string that was SQL connection string (or sql compact). I didn't play with it to much thought so someone else might have a better ideaEse
Its worth a try...but definitely feels like a short term workaround...Gout
yes, that worked to bypass the error, but I don't think its probably a good idea to connect to my EF database without the benefits of EF....?Gout
I had a ton of problems with "simple" membership, probably just me. I had to implement a few of the methods myself because I was using SQL instead of SQL Compact. My membership tables where already in the same db so I just added the tables to my EF model.Ese
Mario's Answer here provides a way to use EF Database / Model First with Simple Membership, albeit with some surgery.Saltwort
C
7

The provider giving you trouble is the one specified in your connection string which is System.Data.EntityClient. I suspect the problem is because your project is database-first whereas the simple membership is using code-first. I do not think you can mix these approaches in a single database. Try putting it back to where the DefaultConnection is used for IntializeDatabaseConnection. There should have been a DefaultConnection in your web.config generated by the MVC4 scaffolding. This connection string usually uses System.Data.SqlClient as the provider.

If you want to keep the simple membership in the database used to store domain information (i.e. DALEntities) then you will need to change your method for using EF on the domain to code-first. If you want to keep your project database-first you need to design your own membership schema in the database and develop custom member and role providers. This is probably the best approach if you are really trying to integrate user information into your domain model.

Childlike answered 15/10, 2012 at 20:15 Comment(4)
I agree with you about the System.Data.EntityClient, but I need to work against my regular database for user management. It is not a workable solution for me to have distinct databases.Gout
Thank you. I understand what you're saying, and I think it answers my question in full.Gout
@KevinJunghans - I don't believe this is true. You should be able to simply have two connection strings to the same database, one (used in WebSecurity) uses SqlClient, the other uses EntityClient.Monseigneur
@MystereMan yes, that does work to have 2 connection strings like you mention.Unreserve
C
1

For simplicity and clarity keep two connection strings pointing to the same database. One for EF (doesn't matter if code first or data first) and one for your WebSecurity stuff. (As Chad and Erik said)

<add name="DataEntities" connectionString="metadata=res://*/Models.DataEntities.csdl|res://*/Models.DataEntities.ssdl|res://*/Models.DataEntities.msl;provider=System.Data.SqlClient;provider connection string='data source=[YOUR_SERVER];initial catalog=[YOUR_DATABASE];integrated security=True;MultipleActiveResultSets=True;App=[YOUR_APP_NAME]'" providerName="System.Data.EntityClient" />
<add name="DataDB" connectionString="data source=[YOUR_SERVER];initial catalog=[YOUR_DATABASE];integrated security=True;MultipleActiveResultSets=True;App=[YOUR_APP_NAME]" providerName="System.Data.SqlClient" />

Note that the providers are different: EF uses System.data.EntityClient and non-EF one uses System.Data.SqlClient.

Columbic answered 3/8, 2014 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.