开发者

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

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:

pub开发者_运维知识库lic 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);


I have to answer my own question.

This works now with the Entity Framework CTP 4

and SQL Server Compact 4.0

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜