System.Data.Entity.Infrastruct.DbUpdateConcurrencyException。存储更新、插入或删除语句影响了意外数量的行 (0)

2023-12-20

Models这些是模型类,我正在使用实体框架。我正在使用这些模型来实现级联下拉列表。

  public class League
    {
        public int Id { get; set; }
        public string League1 { get; set; }
        public string Icon { get; set; }

        public virtual ICollection<LeagueDivision> LeagueDivisions { get; set; }
    }

  public class LeagueDivision
    {
        public int Id { get; set; }
        public Nullable<int> LeagueId { get; set; }
        public string Name { get; set; }
        public string Icon { get; set; }

        public virtual League League { get; set; }
    }  

  public partial class CalculatorPrice
    {
        public int Id { get; set; }
        public int LeagueId { get; set; }
        public int LeagueDivisionId { get; set; }
        public Nullable<decimal> Price { get; set; }
    } 

 public class ViewModelForHostBooster
    {
        [Required(ErrorMessage = "Please enter price")]
        [Display(Name = "Price")]
        public decimal Price { get; set; }       

        [Required(ErrorMessage = "Please select a league")]
        [Display(Name = "League")]

        public int? SelectedLeague { get; set; }
        [Required(ErrorMessage = "Please select a league division")]
        [Display(Name = "League Division")]

        public int? SelectedLeagueDivision { get; set; }

        public SelectList LeagueList { get; set; }
        public SelectList LeagueDivisionList { get; set; }    

    }

控制器/动作在 HttpGet 中,我刚刚填充了级联下拉列表,并且工作正常,现在我正在为此实现 Httppost。我想存储price取决于从下拉列表中选择的列表项以及如果price已经存在,那么我想更新它。第一次可以添加price成功,但是当我第二次尝试更新它时,我得到了System.Data.Entity.Infrastruct.DbUpdateConcurrencyException请任何人指导我如何处理这个问题。

[HttpPost]
 public ActionResult IndexDropDown(ViewModelForHostBooster model)
    {
        if (!ModelState.IsValid)
        {
            ConfigureViewModel(model);
            return View(model);
        }
        else
        {
            HostBoostersDBEntities2 db = new HostBoostersDBEntities2();
            CalculatorPrice calculatePrice = new CalculatorPrice();
             var  calculatePriceExistsOrNot =  db.CalculatorPrices
                .Where(x => x.LeagueId == model.SelectedLeague
                    &&
                    x.LeagueDivisionId == model.SelectedLeagueDivision).ToList();
             if (calculatePriceExistsOrNot.Count > 0)
            {

                calculatePrice.LeagueId = (int)model.SelectedLeague;
                calculatePrice.LeagueDivisionId = (int)model.SelectedLeagueDivision;
                calculatePrice.Price = model.Price;
                db.Entry(calculatePrice).State = EntityState.Modified;

db.SaveChanges(); 行中发生异常“System.Data.Entity.Infrastruct.DbUpdateConcurrencyException”发生在 EntityFramework.dll 中,但未在用户代码中处理。 附加信息:存储更新、插入或删除语句影响了意外数量的行 (0)。自加载实体以来,实体可能已被修改或删除。刷新 ObjectStateManager 条目。

                db.SaveChanges();
            }
            else
            {
                calculatePrice.LeagueId = (int)model.SelectedLeague;
                calculatePrice.LeagueDivisionId = (int)model.SelectedLeagueDivision;
                calculatePrice.Price = model.Price;
                db.CalculatorPrices.Add(calculatePrice);
                db.SaveChanges();
            }


        }
        ConfigureViewModel(model);
        return View(model);
    }

View

  @using (Html.BeginForm("IndexDropDown", "DropDown", FormMethod.Post,
                                      new { enctype = "multipart/form-data" }))
{
    <div>
        @Html.LabelFor(m => m.Price, new { @class ="control-lebel"})
        @Html.TextBoxFor(m => m.Price, new { @class = "form-control"})
        @Html.ValidationMessageFor(m => m.Price)
    </div>

    <div>
        @Html.LabelFor(m => m.SelectedLeague ,new { @class ="control-lebel"})
        @Html.DropDownListFor(m => m.SelectedLeague, Model.LeagueList, new { @class = "form-control"})
        @Html.ValidationMessageFor(m => m.SelectedLeague)
    </div>
    <div>
        @Html.LabelFor(m => m.SelectedLeagueDivision ,new { @class ="control-lebel"})
        @Html.DropDownListFor(m => m.SelectedLeagueDivision, Model.LeagueDivisionList, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.SelectedLeagueDivision)
    </div>
    <input type="submit" value="save" />
}

[HttpPost]
 public ActionResult IndexDropDown(ViewModelForHostBooster model)
    {
        if (!ModelState.IsValid)
        {
            ConfigureViewModel(model);
            return View(model);
        }
        else
        {
            HostBoostersDBEntities2 db = new HostBoostersDBEntities2();
            CalculatorPrice calculatePrice  =  db.CalculatorPrices
                .Where(x => x.LeagueId == model.SelectedLeague
                    &&
                    x.LeagueDivisionId == model.SelectedLeagueDivision).FirstOrDefault();
             if (calculatePrice != null)
            {
                calculatePrice.LeagueId = (int)model.SelectedLeague;
                calculatePrice.LeagueDivisionId = (int)model.SelectedLeagueDivision;
                calculatePrice.Price = model.Price;
                //db.Entry(calculatePrice).State = EntityState.Modified;
                db.SaveChanges();
            }
            else
            {
                calculatePrice = new CalculatorPrice();
                calculatePrice.LeagueId = (int)model.SelectedLeague;
                calculatePrice.LeagueDivisionId = (int)model.SelectedLeagueDivision;
                calculatePrice.Price = model.Price;
                db.CalculatorPrices.Add(calculatePrice);
                db.SaveChanges();
            }


        }
        ConfigureViewModel(model);
        return View(model);
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

System.Data.Entity.Infrastruct.DbUpdateConcurrencyException。存储更新、插入或删除语句影响了意外数量的行 (0) 的相关文章

  • Directory.Delete 之后 Directory.Exists 有时返回 true ?

    我有非常奇怪的行为 我有 Directory Delete tempFolder true if Directory Exists tempFolder 有时 Directory Exists 返回 true 为什么 可能是资源管理器打开了
  • 如何使用 ViewBag 创建 BaseController

    我需要执行以下操作 我已经准备好一些控制器并正在运行 但现在我想创建一个BaseController 我的每一个Controllers应该像这样继承它 public class MySecondController BaseControll
  • 提交后禁用按钮

    当用户提交付款表单并且发布表单的代码导致 Firefox 中出现重复发布时 我试图禁用按钮 去掉代码就不会出现这个问题 在firefox以外的任何浏览器中也不会出现这个问题 知道如何防止双重帖子吗 System Text StringBui
  • 单个对象的 Monogame XNA 变换矩阵?

    我读过一些解释 XNA Monogame 变换矩阵的教程 问题是这些矩阵应用于 SpriteBatch Begin matrix 这意味着所有 Draw 代码都将被转换 如何将变换矩阵应用于单个可绘制对象 就我而言 我想转换滚动背景 使其自
  • 如何创建包含 IPv4 地址的文本框? [复制]

    这个问题在这里已经有答案了 如何制作一个这样的文本框 我想所有的用户都见过这个并且知道它的功能 您可以使用带有 Mask 的 MaskedTestBox000 000 000 000 欲了解更多信息 请参阅文档 http msdn micr
  • 在 C 中初始化变量

    我知道有时如果你不初始化int 如果打印整数 您将得到一个随机数 但将所有内容初始化为零似乎有点愚蠢 我问这个问题是因为我正在评论我的 C 项目 而且我对缩进非常直接 并且它可以完全编译 90 90 谢谢 Stackoverflow 但我想
  • 在 Visual Studio 2010 中从 Fortran 调用 C++ 函数

    我想从 Fortran 调用 C 函数 为此 我在 Visual Studio 2010 中创建了一个 FORTRAN 项目 之后 我将一个 Cpp 项目添加到该 FORTRAN 项目中 当我要构建程序时出现以下错误 Error 1 unr
  • 是否有实用的理由使用“if (0 == p)”而不是“if (!p)”?

    我倾向于使用逻辑非运算符来编写 if 语句 if p some code 我周围的一些人倾向于使用显式比较 因此代码如下所示 if FOO p some code 其中 FOO 是其中之一false FALSE 0 0 0 NULL etc
  • 在一个平台上,对于所有数据类型,所有数据指针的大小是否相同? [复制]

    这个问题在这里已经有答案了 Are char int long 甚至long long 大小相同 在给定平台上 不能保证它们的大小相同 尽管在我有使用经验的平台上它们通常是相同的 C 2011 在线草稿 http www open std
  • 如何检测表单的任何控件的变化?

    如何检测 C 中表单的任何控件的更改 由于我在一个表单上有许多控件 并且如果表单中的任何控件值发生更改 我需要禁用按钮 我正在寻找一些内置函数 事件处理程序 属性 并且不想为此创建自定义函数 不 我不知道任何时候都会触发任何事件any控制表
  • C#:帮助理解 UML 类图中的 <>

    我目前正在做一个项目 我们必须从 UML 图编写代码 我了解 UML 类图的剖析 但我无法理解什么 lt
  • 外键与独立关系 - Entity Framework 5 有改进吗?

    我读过了several http www ladislavmrnka com 2011 05 foreign key vs independent associations in ef 4 文章和问题 https stackoverflow
  • 等待进程释放文件

    我如何等待文件空闲以便ss Save 可以用新的覆盖它吗 如果我紧密地运行两次 左右 我会得到一个generic GDI error
  • AES 128 CBC 蒙特卡罗测试

    我正在 AES 128 CBC 上执行 MCT 如中所述http csrc nist gov groups STM cavp documents aes AESAVS pdf http csrc nist gov groups STM ca
  • 动态添加 ASP.Net 控件

    我有一个存储过程 它根据数据库中存储的记录数返回多行 现在我想有一种方法来创建 div 带有包含该行值的控件的标记 如果从数据库返回 10 行 则 10 div 必须创建标签 我有下面的代码来从数据库中获取结果 但我不知道如何从这里继续 S
  • 如果没有抽象成员,基类是否应该标记为抽象?

    如果一个类没有抽象成员 可以将其标记为抽象吗 即使没有实际理由直接实例化它 除了单元测试 是的 将不应该实例化的基类显式标记为抽象是合理且有益的 即使在没有抽象方法的情况下也是如此 它强制执行通用准则来使非叶类抽象 它阻止其他程序员创建该类
  • 如何部署“SQL Server Express + EF”应用程序

    这是我第一次部署使用 SQL Server Express 数据库的应用程序 我首先使用实体 框架模型来联系数据库 我使用 Install Shield 创建了一个安装向导来安装应用程序 这些是我在目标计算机中安装应用程序所执行的步骤 安装
  • System.IO.FileNotFoundException:找不到网络路径。在 Windows 7 上使用 DirectoryEntry 对象时出现异常

    我正在尝试使用 DirectoryEntry 对象连接到远程 Windows 7 计算机 这是我的代码 DirectoryEntry obDirEntry new DirectoryEntry WinNT hostName hostName
  • C++ 条件编译

    我有以下代码片段 ifdef DO LOG define log p record p else define log p endif void record char data 现在如果我打电话log hello world 在我的代码中
  • 当从finally中抛出异常时,Catch块不会被评估

    出现这个问题的原因是之前在 NET 4 0 中运行的代码在 NET 4 5 中因未处理的异常而失败 部分原因是 try finallys 如果您想了解详细信息 请阅读更多内容微软连接 https connect microsoft com

随机推荐