none
Entity Framework 4.1 with SQL Server CE 4.0 RRS feed

  • Question

  • Hi, i'm trying to create a test project using Entity Framework 4.1 with SQL Server CE 4.0 as the database provider. I have added the reference to the Entity Framework 4.1 dll and added a class called BlogContext. I then have the following config file:

    <configuration>
        <system.web>
            <compilation debug="true" targetFramework="4.0" />
        </system.web>
        <connectionStrings>
            <add name="BlogContext" connectionString="Data Source=Blog.sdf" providerName="System.Data.SqlServerCe.4.0" />
        </connectionStrings>
    </configuration>

    But when i run my tests i get the error:

    "Initialization method EntityFramework4_1.UnitTest.Init threw exception. System.Data.ProviderIncompatibleException: System.Data.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. ---> System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)."

    I'd appreciate it if someone could show me what i'm doing wrong. Thanks

    Tuesday, July 19, 2011 2:24 PM

Answers

  • Problem solved, i removed the connection string and did things differently. Before i call:

    var context = new BlobContext();

    I now say:

    Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");

    And that's all that's needed. Simple when you know how. You may also wish to add the following before the context is created:

    Database.SetInitializer(new DropCreateDatabaseAlways<BlogContext>());

    This is handy for testing as it makes sure the database is dropped. Hope this helps.

    • Marked as answer by Lee Timmins Tuesday, July 19, 2011 2:51 PM
    Tuesday, July 19, 2011 2:51 PM

All replies

  • Problem solved, i removed the connection string and did things differently. Before i call:

    var context = new BlobContext();

    I now say:

    Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");

    And that's all that's needed. Simple when you know how. You may also wish to add the following before the context is created:

    Database.SetInitializer(new DropCreateDatabaseAlways<BlogContext>());

    This is handy for testing as it makes sure the database is dropped. Hope this helps.

    • Marked as answer by Lee Timmins Tuesday, July 19, 2011 2:51 PM
    Tuesday, July 19, 2011 2:51 PM
  • I had exactly the same problem. But I realized it was because the application wasn't using the context class I had created, but another one that was auto-generated named <project name>Context.

    Therefore, as the "working" context class had another name, it wasn't finding the connection string I had defined and it caused the ProviderIncompatibleException. Another symptom was that the initializer class didn't seem to work, since it was made for another class.

    The solution is really simple: just rename the connection string as the auto-generated context class. Just to be sure, you can check the name of the SDF file in the App_Data folder. It's named after the class that's beign used to create it if it didn't have a defined connection string.

    And don't forget to replace every reference to the context class that wasn't beign used.


    The only difference between art and software is that software has to work.
    • Proposed as answer by Jorge Lu Tuesday, January 24, 2012 3:20 PM
    • Edited by Jorge Lu Tuesday, January 24, 2012 3:27 PM
    Tuesday, January 24, 2012 3:19 PM