entity framework连接字符串麻烦
我正在制作一个小型库(DLL)来管理用户及其角色/权限。 该计划是能够将此dll添加到MVC项目并能够操纵用户/角色/等。 所有数据都驻留在SQL数据库中。
我正在使用entity framework进行数据访问。
所以当我初始化一个新的RoleManager(这是我正在制作的lib中的主类的名称)时,我提供了一个connectionString,如下所示:
RoleManager roleManager = new RoleManager(string connectionString);
然后在构造函数中我这样做:
db = new RoleManagerEntities(connectionString); //This is the EntityFramework
我正在尝试提供此连接字符串(以及许多其他字符串)
"metadata=res://*/RoleManager.csdl|res://*/RoleManager.ssdl|res://*/RoleManager.msl;provider=System.Data.SqlClient;provider connection string='Data Source=localhost;Initial Catalog=Login;Integrated Security=True;Connection Timeout=60; multipleactiveresultsets=true'"
我收到以下错误:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
这个问题是我试图从我的新项目中实例化EF而不提供连接字符串并且我的应用程序配置中没有任何内容的默认值。 太糟糕了我现在无法删除它。
只需将DLL配置文件中的连接字符串信息复制到可执行配置文件即可。
基本上,您尝试通过此ObjectContext构造函数(String)实例化ObjectContext ,而不传递其预期格式的字符串参数,这就是问题所在。
这是你需要做的:
1.首先在您的“测试项目”app.config中创建一个条目,因为这是CLR在运行时查找连接字符串的位置。
2.现在更改代码以传递连接字符串名称而不是实际的连接字符串:
db = new RoleManagerEntities("name=RoleManagerEntities");
构造函数可能正在web.config的connectionStrings设置中查找连接字符串,其中包含您将其作为参数传递的名称。
所以如果你打电话:
db = new RoleManagerEntities("Foobar");
它正在寻找:
我不肯定这是解决方案,但这就是错误消息似乎表明的内容。
我不是EF的专家,但我不认为连接字符串是有效的。 尝试:
metadata=res://*;provider=System.Data.SqlClient;provider connection string='Data Source=localhost;Initial Catalog=Login;Integrated Security=True;Connection Timeout=60; multipleactiveresultsets=true'