Alex Rivera | Logout

Entity Framework connection string not from config

Asked 2012-03-06T10:52:11.500
19
public class Context : DbContext
{
    public Context(string connString) : base(connString) 
    {
        Database.SetInitializer(new MyContextInitializer());
    }
//...

Need to pass a connection string to the context constructor. How should the string look like, for example, for SQL Compact? Tried this, but no success:

Context db = new Context("Provider=System.Data.SqlServerCe.4.0;Data Source=D:\\Context.sdf");

Edit:

If I try this string: "Data Source=D:\\Context.sdf"

System.Data.ProviderIncompatibleException was unhandled

Message=An error occurred while getting provider information from the database.
This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.

Source=EntityFramework

And if I try to mention the provider like this: "Data Source=D:\\Context.sdf;provider=System.Data.SqlServerCe.4.0"

System.ArgumentException was unhandled

Message=Keyword not supported: 'provider'.

Source=System.Data

Edit
Report

1 Answer

5

I suggest you to use always the EntityConnectionStringBuilder (System.Data.EntityClient):

EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();
ecsb.Provider = "System.Data.SqlServerCe.4.0";
ecsb.Metadata = "..."; // your metadata
ecsb.ProviderConnectionString = "Data Source=D:\\Context.sdf";

then you can generate the connection string in a simple way:

Context db = new Context(ecsb.ToString());

UPDATE:

Try to create the EntityConnection, then pass it to the context, instead of connection:

EntityConnection conn = new EntityConnection(ecsb.ToString());
Context db = new Context(conn);

Anyway, where is the metadata? It's required by the EntityConnection!

answered 2012-03-06T11:25:42.197

Your Answer