Question

With VS 2010 SP1 I created an ASP.NET MVC4 project from the "internet" template. I then created a connection string for SQL Server CE 4.0:

<add name="DefaultConnection"
     connectionString="Data Source=|DataDirectory|MyDatabase.sdf;Password=123456" 
     providerName="System.Data.SqlServerCe" /> 

With the web application successfully debug-launched in cassini, I choose the "Register" user option. Immediately this causes the InitializeSimpleMembershipAttribute filter to execute. The filter crashes the site when it reaches this code:

Database.SetInitializer<UsersContext>(null);
using (var context = new UsersContext())
{
    if (!context.Database.Exists())
    {
        // Create the SimpleMembership database without Entity Framework migration schema
        ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
    }
}
//This line never executes.  It is meant to configure my custom user table.
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "Users", "ID", "UserName", true);

The Exists() check throws an ArgumentException stating:

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

Now I wanted to be sure that (1) there was nothing wrong with my connection string and (2) there was nothing wrong with my provider. To do that, I inserted a snippet of code before the Exists() check. The snippet used a new SqlCeEngine to create the database, and Dapper calls to setup user tables. That code worked just fine, just before exploding on the Exists() check again.

I then considered that EF might need some additional setup help. I tried replacing the EF defaultConnectionFactory in my web.config:

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

Still the exception I received did not change.

I'm now wondering if EF needs a "special" connection string to work with SQL Server CE. I will be checking here and here. But at first glance I'm not sure that is the issue.

Thoughts?

Was it helpful?

Solution

EF requires the providers to be installed. Use one the connect to DB options to check. eg ADD Entity DAta Model to Project. Just to the providers available to it. Do you see your preferred connection Client ? enter image description here

Seems Compact edition should work with some restrictions... http://technet.microsoft.com/en-us/library/cc835494.aspx

So then I think its The "DEFAULT CONNECTION" issue See the Context constructor. EF looks for a connection by that name unless you pass an alternative in. try...

<connectionStrings>
    <add name="MyContextName" connectionString="bla";App=EntityFramework"
       providerName="System.Data.SqlServerCe.4.0" />

OTHER TIPS

You need to install Sql Server Compact edition. You can download it from here.

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