代码之家  ›  专栏  ›  技术社区  ›  Jason

实体框架连接字符串故障

  •  8
  • Jason  · 技术社区  · 14 年前

    我使用实体框架进行数据访问。

    因此,当我初始化一个新的RoleManager(这是我正在创建的库中的主类的名称)时,我为它提供了一个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而没有提供连接字符串,也没有在我的应用程序配置中设置默认值。可惜我现在不能删除它。

    4 回复  |  直到 9 年前
        1
  •  8
  •   Dean Kuga    14 年前

    只需将连接字符串信息从DLL配置文件复制到可执行配置文件。

        2
  •  6
  •   Morteza Manavi    14 年前

    基本上,您试图实例化 ObjectContext 通过这个 ObjectContext Constructor (String)
    以下是您需要做的:

    一。首先在“test project”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>
    

    名称 而不是实际的连接字符串:
    db = new RoleManagerEntities("name=RoleManagerEntities");
    
        3
  •  0
  •   Dismissile    14 年前

    构造函数可能正在web.config的connectionStrings设置中查找一个连接字符串,该字符串的名称是作为参数传递的。

    db = new RoleManagerEntities("Foobar");
    

    它正在寻找:

    我不确定这是否是解决方案,但错误信息似乎表明了这一点。

        4
  •  0
  •   Phil Sandler    14 年前

    我不是EF方面的专家,但我不认为该连接字符串有效。尝试:

    metadata=res://*;provider=System.Data.SqlClient;provider connection string='Data Source=localhost;Initial Catalog=Login;Integrated Security=True;Connection Timeout=60; multipleactiveresultsets=true'