entity framework连接字符串不是来自配置

public class Context : DbContext { public Context(string connString) : base(connString) { Database.SetInitializer(new MyContextInitializer()); } //... 

需要将连接字符串传递给上下文构造函数。 字符串应该如何,例如,对于SQL Compact? 试过这个,但没有成功:

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

编辑:

如果我尝试这个字符串: "Data Source=D:\\Context.sdf"

System.Data.ProviderIncompatibleException未处理

Message =从数据库获取提供程序信息时发生错误。
这可能是由entity framework使用不正确的连接字符串引起的。 检查内部exception以获取详细信息,并确保连接字符串正确。

来源=的EntityFramework

如果我尝试提到这样的提供程序: "Data Source=D:\\Context.sdf;provider=System.Data.SqlServerCe.4.0"

System.ArgumentException未处理

消息=不支持关键字:’provider’。

来源= System.Data

我首先遇到了与MVC 4代码类似的错误(运行update-database时)。 我得到的错误:

从数据库获取提供程序信息时发生错误。 这可能是由entity framework使用不正确的连接字符串引起的。 检查内部exception以获取详细信息,并确保连接字符串正确。

事实certificate,我在web.config中遗漏了一些关键信息,以便全部使用localDB。 这是重要的部分(我使用了http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-configuration-file-settings.aspx中的参考资料):

   

好的方法,这是我的整个web.config(我使用的是MVC 4,EF 4.3.1,EF-Migrations):

    

我建议你总是使用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"; 

然后你可以用一种简单的方式生成连接字符串:

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

更新:

尝试创建EntityConnection,然后将其传递给上下文,而不是连接:

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

无论如何,元数据在哪里? 这是EntityConnection的要求!

在我的情况下(完全相同的错误)它通​​过更改默认连接工厂解决:

 Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"); 

在连接字符串中省略对提供者的任何引用,因为没有必要(这种方式)。 我想我读过默认情况下,Database.DefaultConnectionFactory设置为SqlConnectionFactory(),但我找不到它的引用。

我有同样的问题,它已通过以下方式解决:

1-将管理器控制台中的默认项目设置为包含所需web.config的项目

2-将解决方案的启动项目设置为同一项目,以便update命令查找连接字符串。