Unable to determine the provider name for connection of type 'System.Data.SqlServerCe.SqlCeConnectio

Posted by Hobbes1312 on Stack Overflow See other posts from Stack Overflow or by Hobbes1312
Published on 2010-05-10T15:52:46Z Indexed on 2010/05/16 1:10 UTC
Read the original article Hit count: 1275

Filed under:
|

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);

© Stack Overflow or respective owner

Related posts about ADO.NET

Related posts about sql-server-ce