【发布时间】:2015-04-30 22:37:02
【问题描述】:
我有一个 Web 应用程序,每当我将其部署到 Azure 时,它都会返回以下错误:
System.Data.SqlClient.SqlException:“ON”附近的语法不正确
它告诉我错误来自我的 _Layout.cshtml 中的第 45 行:
@Html.Action("LoginPartial", "Home")
现在,在HomeController 我有以下内容:
[AllowAnonymous]
[ChildActionOnly]
public ActionResult LoginPartial()
{
var userId = User.Identity.GetUserId();
var model = context.Users.Find(userId);
// In case no such user is found and the model is called
if (model == null)
{
model = new ApplicationUser()
{
FirstName = "N/A",
LastName = "N/A"
};
}
return PartialView("~/Views/Shared/_LoginPartial.cshtml", model);
}
...每当我在本地调试应用程序时,它都能正常工作 - 即使是远程连接的数据库(它们从 Azure 中的 SQL Server 运行)。
我使用此代码的目的是从共享视图 (_LoginPartial.cshtml) 显示登录用户的名字和姓氏,正如我所说,它在本地运行良好 - 但每次我将其发布到 Azure 时都会崩溃。
如果我注释掉 LoginPartial() 方法中的前两行,问题就会消失。但是话又说回来,该方法的目的也是如此……显然,从数据库中访问数据存在问题。
有什么想法吗?
【问题讨论】:
-
什么是
context,什么是.Users,什么是.Find()? -
context 是 AspNetUsers 表所在的数据库上下文 (ApplicationDbContext),.Users 用于访问数据库中的用户表,.Find() 返回具有给定 UserID 的 ApplicationUser 对象。这一切都是微软的一部分。 AspNet.Identity 和我自己写的什么都没有。我已经使用断点调试了这一部分,并且在本地运行时,它按预期从数据库上下文中检索 ApplicationUser 对象。
-
您的数据库提供商是什么(在 entityFramework-section 中配置)?
-
我不太清楚你所说的 entityFramework-section 是什么意思,但如果你说的是 web.config 文件,它就是这样写的:
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />在连接字符串中:@ 987654328@
标签: asp.net sql-server azure asp.net-mvc-5 azure-sql-database