0

我收到错误“底层提供程序在打开时失败。” 实际上我没有调用实体框架连接字符串我的代码是,

 InspectionEntities dbContext = new InspectionEntities();

当“InspectionEntities”为时如何传递实体框架连接字符串,

namespace DataAccessManager
{
    public class InspectionEntities : ObjectContext
    {
        public InspectionEntities();
        public InspectionEntities(EntityConnection connection);
        public InspectionEntities(string connectionString);

和检查是,

[assembly: EdmSchemaAttribute()]

namespace DataAccessManager
{
    #region Contexts

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public partial class InspectionEntities : ObjectContext
    {
        #region Constructors

        /// <summary>
        /// Initializes a new InspectionEntities object using the connection string found in the 'InspectionEntities' section of the application configuration file.
        /// </summary>
        public InspectionEntities() : base("name=InspectionEntities", "InspectionEntities")
        {
            OnContextCreated();
        }

        /// <summary>
        /// Initialize a new InspectionEntities object.
        /// </summary>
        public InspectionEntities(string connectionString) : base(connectionString, "InspectionEntities")
        {
            OnContextCreated();
        }

        /// <summary>
        /// Initialize a new InspectionEntities object.
        /// </summary>
        public InspectionEntities(EntityConnection connection) : base(connection, "InspectionEntities")
        {
            OnContextCreated();
        }

        #endregion

我的连接字符串是

<add name="InspectionEntities" connectionString="metadata=res://*/Inspection.csdl|res://*/Inspection.ssdl|res://*/Inspection.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=*****\SQLEXPRESS;Initial Catalog=Inspection;User ID=sa;Password=****;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

InspectionEntities dbContext = new InspectionEntities();

当我使用它的对象 dbcontext 获取数据时,它给了我错误,因为基础提供程序在打开时失败

如何在这里传递或调用实体连接字符串,

InspectionEntities dbContext = new InspectionEntities("name=InspectionEntities");???

我在这里也一样

希望你的建议

提前致谢

4

1 回答 1

1

您不必将连接字符串传递给上下文构造函数。而是在web.config文件中更改它。

您收到的错误表明 SQL 服务器名称或实例名称指定不正确,或者防火墙阻止了连接。如果是用户名/密码问题,您会收到一条错误消息,指出无法登录。

于 2013-05-13T13:11:13.927 回答