Entity framework Azure网站异常:初始化字符串的格式不符合从索引0开始的规范

Entity framework Azure网站异常:初始化字符串的格式不符合从索引0开始的规范,entity-framework,connection-string,azure-web-app-service,Entity Framework,Connection String,Azure Web App Service,我有一个Azure网站,用来托管我的MVC+实体框架项目。当我使用与我的SQL Azure数据库相同的连接在本地运行该站点时,一切都可以正常运行,但当我将该站点部署到我的Azure网站时,我会遇到以下错误: 初始化字符串的格式不符合从索引0开始的规范 我按照其他帖子中的步骤,将连接字符串添加到抛出的错误中,连接字符串就是这样的: Server=tcp:abcde1fgh2.database.windows.net,1433;Database=SalesLeads;User ID=username

我有一个Azure网站,用来托管我的MVC+实体框架项目。当我使用与我的SQL Azure数据库相同的连接在本地运行该站点时,一切都可以正常运行,但当我将该站点部署到我的Azure网站时,我会遇到以下错误:

初始化字符串的格式不符合从索引0开始的规范

我按照其他帖子中的步骤,将连接字符串添加到抛出的错误中,连接字符串就是这样的:

Server=tcp:abcde1fgh2.database.windows.net,1433;Database=SalesLeads;User ID=username_test@abcde1fgh2;Password=Password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;
此字符串与本地使用的连接字符串匹配。
为什么这可以在本地使用相同的字符串,但不能在远程使用

编辑 以下是该网站的完整web.config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Server=tcp:abcde1fgh2.database.windows.net,1433;Database=SalesLeads;User ID=username_test@abcde1fgh2;Password=Password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" maxRequestLength="102400" />
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.AspNet.SignalR.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.2.0" newVersion="2.1.2.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

通过对此的一些研究,我发现错误发生在以下可能的情况中:

可能性1:您可能使用了错误的
DbConnection
类。如果您使用的是Access,请确保您使用的是
OleDbConnection
,而不是
SqlConnection
。。。反之亦然

可能性2:这表明该问题在某种程度上与数据库相关,但该问题与在IIS上运行网站的应用程序池标识方式有关。这里有一些关于这意味着什么的背景。我们注意到,在以更老式的方式配置的站点上,我们往往看不到问题,即在服务器上创建一个新的“IUSR”用户帐户,然后将此用户添加到IIS用户组,然后将站点设置为使用此用户而不是应用程序池标识确保密码过期问题在服务器上的主管理员帐户上排序。如果这不能解决问题,请创建一个新的“IUSR”Windows用户帐户,并将站点设置为使用此帐户而不是应用程序池标识在共享主机上,这可能是不可能的,但处理管理员密码过期问题仍然是主机可以解决的问题。

您应该进入Azure网站,并在
web.config
中检查您的连接字符串


当你发布你的网站时,你的
web.config
会被Azure转换/修改,因此这个过程可能会把你的连接字符串搞砸,从而导致这个错误。例如,如果您通过门户网站设置连接字符串,可能在复制/粘贴过程中编码出错,等等。

10/2/17-使用Azure web App+ASP.NET MVC(.NET 4.6.1)+实体框架(最新版本)。为了快速修复,我将连接字符串放在代码中,它成功了:

public class MyDbContext : DbContext
{
    private const string connectionString = "Server=tcp:MYDB.database.windows.net,1433;Initial Catalog=MYDBNAME;Persist Security Info=False;User ID=MYUSERID;Password=MYPW;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
    public MyDbContext() : base(connectionString)
    {

    }
} 

在我的例子中,我忘了将SQL_URI封装在

@Microsoft.KeyVault(SecretUri=SQL\u URI)


这发生在我的Web应用程序>配置>连接字符串>值中

请将整个连接字符串放在此处。已添加完整的Web.configural您可以访问Azure网站并在那里检查您的连接字符串并将其发布到此处吗?当你发布你的网站时,你的web.config会被Azure转换,所以这个转换过程可能会弄乱你的连接字符串。呸@内梅斯夫,这成功了!快点,回答我好让我给你150分!转换中一定有一些错误(比如非ascii引号或其他东西)。但是查询字符串看起来完全相同。。。真奇怪。谢谢你的建议。至于可能性1,我使用的是Azure SQL和实体框架。我的本地站点副本使用同一个连接到同一个SQLAzure数据库,并且工作正常(我将编辑我的问题,使这一点更加明显)。此问题仅发生在Azure网站中托管的网站上。至于可能性2,db具有SQL身份验证,因此我不确定IIS应用程序池标识是否与错误有很大关系?我还是会调查这件事,看看它会把我引向何方!大笑。我并不是真的想在我的源代码中输入密码。我用它来制作原型。