7

I'm playing around with SQL Server Compact Edition 4 CTP1 as I'd like to use it as a datastore for a low-traffic web app. When I try to create a DataContext with a connection string specifying System.Data.SqlServerCe.4.0 (in order to use LINQ To SQL), I get the following error message:

Cannot open '|DataDirectory|\data.sdf'. Provider 'System.Data.SqlServerCe.3.5' not installed.

So why is my code not using version 4 of SQL CE?

Back story: I'm using Visual Web Developer Express 2010 for development, but I downloaded the WebMatrix beta and used its designer to create a SQL CE 4 .sdf file containing some test data.

Using the SqlCeConnection/SqlCeCommand/SqlCeDataReader classes, I've successfully created a basic MVC app which retrieves the test data and displays it. The SQL CE 4 binaries are copied into the app's bin folder. In my controller:

var connectionString = ConfigurationManager.ConnectionStrings["Main"].ConnectionString;
var tmp = new Dictionary<string, string>();

using(var conn = new SqlCeConnection(connectionString))
{
    conn.Open();

    using (SqlCeDataReader r = new SqlCeCommand("select * from ttest", conn).ExecuteReader())
    {
        while (r.Read())
        {
            tmp.Add(r["id"].ToString(), r["name"].ToString());
        }
    }
}

return View(new TestViewModel { 
    Items = tmp
});

The connection string in Web.config is as follows:

<add name="Main" connectionString="Data Source=|DataDirectory|\data.sdf" providerName="System.Data.SqlServerCe.4.0" />

This works perfectly, so I know the connection string is correct and that I've set up the binaries properly etc. So I thought I'd try out a bit of LINQ To SQL stuff, which is how I want to build the real app:

[Table(Name = "tTest")]
public class TestEntity
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true)]
    public int ID { get; set; }
    [Column]
    public string Name { get; set; }
}

public class Repository
{
    private Table<TestEntity> testTable;

    public Repository()
    {
        var connectionString = ConfigurationManager.ConnectionStrings["Main"].ConnectionString;
        var context = new DataContext(connectionString);
        testTable = context.GetTable<TestEntity>();
    }

    public IQueryable<TestEntity> TestEntities
    {
        get { return testTable;  }
    }
}

And then in the controller (db being a Repository constructed in the controller constructor):

var tmp = db.TestEntities.ToDictionary(x => x.ID.ToString(), x => x.Name);

return View(new TestViewModel { 
    Items = tmp
});

But when I view the page using this code, I get the aforementioned error:

Cannot open '|DataDirectory|\data.sdf'. Provider 'System.Data.SqlServerCe.3.5' not installed.

How can I force my app to use the correct version? Can anyone help?

Mark Bell
  • 28,985
  • 26
  • 118
  • 145
  • I am also trying to do the same, but result is bit different, error message is that ASP.NET doesn't support SQLCE. Though, I am using SQLCE 4.0 RTM. If you are successful pl. let me know. –  Jan 25 '11 at 18:30

1 Answers1

7

LINQ to SQL is not supported by SQL Server Compact 4.0, only Entity Framework / LINQ to Entities. But if you pass a version 4 SqlCeConnection to the DataContext constructor, it will actually work!

ErikEJ
  • 40,951
  • 5
  • 75
  • 115
  • Ok, thanks - [your article](http://erikej.blogspot.com/2010/11/using-entity-framework-with-sql-server.html) was very helpful too. Am I right in thinking that LINQ to SQL was supported by SQL CE 3.5 though? And if so, why did they remove the support? – Mark Bell Dec 04 '10 at 15:43
  • Thanks! Yes, LINQ to SQL was supported by SQL CE 3.5. Priorities and support for new features only available in EF (model first / code fist), and better support for EF with ASP.NET, OData, Dynamic Data etc. – ErikEJ Dec 05 '10 at 16:29
  • 2
    And here's the supporting link saying SQL CE 4 doesn't work with Linq to SQL: http://blogs.msdn.com/b/sqlservercompact/archive/2011/01/12/microsoft-sql-server-compact-4-0-is-available-for-download.aspx – mattmc3 Mar 15 '11 at 13:02