我使用本地数据库在本地构建了一个ASP.NET MVC5网站。我得到了一点我想把它带到azure web服务上,所以我启用了一个Azure网站并设置从Visual Studio Online Git repo进行部署。部署发生但访问该页面只会在处理您的请求时生成“错误”。错误。
如果我查看日志,我会收到以下错误:
- MODULE_SET_RESPONSE_ERROR_STATUS
醇>警告ModuleName =" ManagedPipelineHandler", 通知=" EXECUTE_REQUEST_HANDLER",HttpStatus =" 500", HttpReason ="内部服务器错误",HttpSubStatus =" 0",ErrorCode =" The 操作成功完成。 (0x0)",ConfigExceptionInfo =""
数据库的连接字符串已替换为AzureSqlDatabase连接字符串,因此我尝试在那里进行检查。字符串'看起来'很好,但我注意到,当我在调试模式下运行应用程序时,它以某种方式使用原始的本地Sql数据库,尽管连接字符串不再在代码中!
我已经尝试了SO中列出的一些建议,包括启用详细日志记录和重新推送web.config文件,但似乎没有任何效果。关于如何追踪并解决此问题的任何想法?
编辑: 在MFanto的帮助下,我发现应用程序仍然以某种方式使用旧的本地SQL数据库的连接字符串。 (数据源=。\ SQLEXPRESS;初始目录= HouseOfBurt.Models.DataContext; Integrated Security = True; MultipleActiveResultSets = True)
Web.config如下:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="Application" />
<add namespace="Application.Models" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*.cshtml" verb="*"
preCondition="integratedMode"
type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
Web.Debug.Config(当然是密码修改)
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<connectionStrings>
<add name ="DataContextContainer" connectionString="Server=tcp:r0c0umg8th.database.windows.net,1433;Database=HouseOfBurt;User ID=dba@r0c0cmg8th;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.EntityClient"/>
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your Web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</configuration>
EDIT2:添加DbContext类:
public class DataContext : DbContext
{
#region Tables
public DbSet<Article> Articles { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Link> Links { get; set; }
#endregion Tables
#region Public Methods
#endregion Public Methods
}
答案 0 :(得分:4)
您发布的DbContext
班级名为DataContext
,但示例中的连接字符串为DataContextContainer
。
如果未显式提供连接字符串,Entity Framework将查找名称与完全匹配的连接字符串到DBContext的名称; else惯例将尝试使用基于代码的连接字符串,以SQLExpress为目标。
更改连接字符串名称似乎解决了这个问题。
有关与实体框架连接字符串相关的约定的更多信息,请访问http://msdn.microsoft.com/en-us/data/jj592674。