IIS中出现WCF EntityFrameWork异常

本文关键字:EntityFrameWork 异常 WCF IIS | 更新日期: 2023-09-27 17:57:40

我需要帮助配置通过EF访问SQL数据库的WCF服务。当我通过visualstudio在本地启动该服务时,该服务运行良好,但是,当我将其部署到另一台服务器并通过客户端应用程序访问它时,我不断收到"{"从数据库获取提供程序信息时出错。这可能是由于实体框架使用了不正确的连接字符串造成的。有关详细信息,请检查内部异常,并确保连接字符串正确。"}"错误。

在我将服务发布到服务器之前,我的连接字符串是:

<configSections>
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
</entityFramework>
<connectionStrings>
  <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)'v11.0;Initial Catalog=aspnet-ECUWebUi-20121116095239;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|'aspnet-ECUWebUi-20121116095239.mdf" />
</connectionStrings>

我发布后的连接字符串:

<configSections>
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
<connectionStrings>
  <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=localhost; Integrated Security=SSPI; Initial Catalog=ECUWeb-Staging;User ID=user;Password=password" />
</connectionStrings>

这是对服务中EF上下文的调用:

public RegisterResponse Register(RegisterRequest request)
{
    EfDataAccessFactory factory = new EfDataAccessFactory(new DtoMapper());
    RegisterResponse response = new RegisterResponse();
    if (request.Computer.Exists())
    {
        factory.ComputerDataAccess.Update(request.Computer);
        response.Message = string.Format("{0} is already registered.", request.Computer.Name);
    }
    else
    {
        request.Computer = factory.ComputerDataAccess.Insert(request.Computer);
        response.Message = string.Format("{0} registered with id: {1}", request.Computer.Name, request.Computer.Id);
    }
    RegisterInstances(request);
    return response;
}

有什么建议吗?此外,内部异常为null。

IIS中出现WCF EntityFrameWork异常

修复了我必须添加的问题:

<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=localhost;Initial Catalog=ECUWeb-Staging;User Id = user; Password = password" />
    <add name="EcuWeb.Data.EcuWebDataContext" connectionString="Data Source=localhost;Initial Catalog=ECUWeb-Staging; User Id = user; Password = password" providerName="System.Data.SqlClient" />

然后它就没问题了。