部署到 Azure 时:System.Data.SqlClient.SqlException:'ON' 附近的语法不正确

When deploying to Azure: System.Data.SqlClient.SqlException: Incorrect syntax near 'ON'

我有一个 Web 应用程序 returns 每当我将它部署到 Azure 时出现以下错误:

System.Data.SqlClient.SqlException: Incorrect syntax near '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 服务器)。

我使用此代码的目的是从共享视图 (_LoginPartial.cshtml) 显示登录用户的名字和姓氏,正如我所说,它在本地运行良好 - 但每次发布时都会崩溃到 Azure。

如果我注释掉 LoginPartial() 方法中的前两行,问题就会消失。但是话又说回来,该方法的目的也是如此......所以显然从数据库访问数据存在问题。

有什么想法吗?

我终于弄明白为什么它无法从 Azure 运行。

在部署解决方案和数据库时,我发现不支持用户实例化,因此未能成功部署数据库。

通过在 SQL 服务器上手动创建数据库,然后从 "Publish Web" 向导中选择它而不是附加数据库文件,应用程序可以从 Azure 运行ning 正常运行.

我还为发布 Web 向导中的每个数据库选择了 "Execute Code First Migrations"。不知道这是否与此有关,但可能值得一提。