实体框架连接字符串麻烦

Jas*_*son 8 c# sql entity-framework

我正在制作一个小型库(DLL)来管理用户及其角色/权限.该计划是能够将此dll添加到MVC项目并能够操纵用户/角色/等.所有数据都驻留在SQL数据库中.

我正在使用实体框架进行数据访问.

所以当我初始化一个新的RoleManager(这是我正在制作的lib中的主类的名称)时,我提供了一个connectionString,如下所示:

RoleManager roleManager = new RoleManager(string connectionString);
Run Code Online (Sandbox Code Playgroud)

然后在构造函数中我这样做:

db = new RoleManagerEntities(connectionString); //This is the EntityFramework
Run Code Online (Sandbox Code Playgroud)

我正在尝试提供此连接字符串(以及许多其他字符串)

"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'"
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
Run Code Online (Sandbox Code Playgroud)

这个问题是尝试从我的新项目中实例化EF而不提供连接字符串并且我的应用程序配置中没有任何内容,因为它默认为.太糟糕了我现在无法删除它.

Dea*_*uga 8

只需将DLL配置文件中的连接字符串信息复制到可执行配置文件即可.


Mor*_*avi 6

基本上,您尝试通过此ObjectContext构造函数(String)实例化ObjectContext,而不传递其预期格式的字符串参数,这就是问题所在. 以下是您需要做的事情: 1.首先在您的"测试项目"app.config中创建一个条目,因为这是CLR在运行时查找连接字符串的位置.


<configuration>
  <connectionStrings>
    <add name="RoleManagerEntities" connectionString="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'" />
  </connectionStrings>
</configuration>

2.现在更改代码以传递连接字符串名称而不是实际的连接字符串:

db = new RoleManagerEntities("name=RoleManagerEntities");
Run Code Online (Sandbox Code Playgroud)