views:

239

answers:

2

Hi,

i am using the Ado.Net Entity Framework with Code Only (Tutorial at: ADO.NET team blog) and i want to be as much database independent as possible.

In my first approach i just want to go for Sql Express and Sql Compact databases. With Sql Express everthing works fine but with Sql Compact i get the exception mentioned in my question.

Does anybody knows if it is possible to connect to Sql Compact with the Code Only approach? (with a generated .edmx file for a Sql Compact database everthing works fine, but i want to use code only!)

Here is some code:

My Class which is building the DataContext:

public class DataContextBuilder : IDataContextBuilder
{
    private readonly DbProviderFactory _factory;

    public DataContextBuilder(DbProviderFactory factory)
    {
        _factory = factory;
    }

    #region Implementation of IDataContextBuilder

    public IDataContext CreateDataContext(string connectionString)
    {
        var builder = new ContextBuilder<DataContext>();
        RegisterConfiguration(builder);

        var connection = _factory.CreateConnection();
        connection.ConnectionString = connectionString;

        var ctx = builder.Create(connection);

        return ctx;
    }

    #endregion

    private void RegisterConfiguration(ContextBuilder<DataContext> builder)
    {
        builder.Configurations.Add(new PersonConfiguration());
    }
}

The line

var ctx = builder.Create(connection);

is throwing the exception.

The IDataContext is just a simple Interface for the ObjectContext:

public interface IDataContext
{
    int SaveChanges();

    IObjectSet<Person> PersonSet { get; }
}

My connection string is configured in the app.config:

<connectionStrings>
<add name="CompactConnection" connectionString="|DataDirectory|\Test.sdf"
  providerName="System.Data.SqlServerCe.3.5" />
</connectionStrings>

And the build action is started with

var cn = ConfigurationManager.ConnectionStrings["CompactConnection"];
var factory = DbProviderFactories.GetFactory(cn.ProviderName);
var builder = new DataContextBuilder(factory);
var context = builder.CreateDataContext(cn.ConnectionString);
A: 

Hi ) I got a same Exception. I think this is because Code Only still under development.

Drunken Drinken
A: 

I have to answer my own question.

This works now with the Entity Framework CTP 4

and SQL Server Compact 4.0

Hobbes1312
you can download Sql Server Compact 4.0 at http://blogs.msdn.com/b/sqlservercompact/archive/2010/07/07/introducing-sql-server-compact-4-0-the-next-gen-embedded-database-from-microsoft.aspx
Hobbes1312

related questions