【问题标题】:Cannot successfully delete record from the database无法从数据库中成功删除记录
【发布时间】:2012-10-19 20:23:39
【问题描述】:

我做错了吗?一切似乎都正常,但记录并没有从数据库中删除:

C#

    @{
    WebSecurity.RequireAuthenticatedUser();

    myModel.OSFEntities database = new myModel.OSFEntities();

    var productInformation = Request["Pi"];

    int productId = Convert.ToInt32(productInformation.Substring(0, productInformation.LastIndexOf("-")));
    string productType = productInformation.Substring(productInformation.LastIndexOf("-", productInformation.Length)).Replace("-", String.Empty);
    var userId = WebSecurity.CurrentUserId;
    DateTime date = DateTime.Now;
    int quantity = 1;
    string quoteId = "";

    if (Request["Action"] == "Remove")
    {
        if (Session["QuoteId"] != null)
        {
            quoteId = (String)Session["QuoteId"];

            myModel.Quote quotes = database.Quotes.FirstOrDefault(
                q => q.UserId == userId && q.QuoteId == quoteId && q.ProductId == productId && q.ProductType == productType);

            database.Quotes.DeleteObject(quotes);
            database.SaveChanges();
        }
    }
}

打字稿:

function RemoveProductFromCart(e) {
    // Remove from their Cart.
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                // The data is now stored in the responseText.
                // Change value in textbox to 0 so user knows it's been
                // removed from their cart.
                var el = <HTMLInputElement>document.getElementById(e.id + "-TB");
                if (el != null) {
                    el.value = "0";
                }
            }

            else {
                // Server responded with a different response code.
                alert(xhr.statusText);
            }

        }
    }

    var parameters = "Pi=" + encodeURIComponent(e.id) + "&" + "Action=Remove";

    xhr.open("POST", "../Cart.cshtml");
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.setRequestHeader("Content-length", parameters.length.toString());
    xhr.setRequestHeader("Connection", "close");
    xhr.send(parameters);

    return e.id;
}

在使用 F12 开发工具进行调试时,我看到了:

... 这让我相信我的 C# 代码有问题。为什么它不会从数据库中删除?

【问题讨论】:

  • 我会强烈建议您将所有业务逻辑从您的视图中移出并至少控制器...
  • 抱歉,上面的代码看起来很像 MVC 语法。您是否验证过您对报价的查询实际上返回了一些东西而不是空值?
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 我在这里已经 3 年多了,还没有看到人们在使用标签时遇到问题。如果他们不阅读您的问题,那么他们将不会有太大帮助。
  • 这句话有子关系吗?或引用此报价的任何实体?如果有,需要先删除

标签: c# javascript asp.net entity-framework typescript


【解决方案1】:

我有一些建议。

在这些情况下,很可能其中一个请求项与您认为的不完全一致,因此操作或报价 ID 都可能包含惊喜。您可以使用断点检查那些。

下一个要检查的项目是myModel.Quote quotes 包含匹配的引用对象。即使 id 是正确的(例如,它没有链接到当前用户,或者产品类型不同等),您那里的过滤器之一也可能会排除它。

我注意到您正在使用...

database.Quotes.DeleteObject(quotes);
database.SaveChanges();

即您在第一个而不是第二个上使用 QuotesSaveChangesDeleteObject 调用不应该针对相同的上下文放置吗?

【讨论】:

  • 啊,我明白了。是的,这确实很有意义。我最初准备了一篇关于在数据库中保存更改的文章,这就是他们的代码的方式,并且它一直有效,所以我并没有真正看到它有什么问题。不过,关于惊喜,你是对的。设置断点后(由于某种原因我忘了你可以这样做)数量前有一个前导空格。我也很惊讶没有抛出任何异常。谢谢你,索尼。 :)
猜你喜欢
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多