插入语句影响了意外的行数 (0)

insert statement affected an unexpected number of rows (0)

我正在尝试使用 Entity Framework Code First 插入记录。尝试保存记录时,我不断收到以下错误。

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

型号:

 [Table("tblActual_CategoryItem")]
    public class Actual_CategoryItem
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        public int? AgencyID { get; set; }
        public string UniqueURL { get; set; }
        public string Name { get; set; }
        public string Price { get; set; }
        public string Location { get; set; }
        public string LastUpdated { get; set; }
        public string Description { get; set; }
        public string PhotoURL1 { get; set; }
        public string PhotoURL2 { get; set; }
        public string AgencyName { get; set; }
        public string AgencyUniqueURL { get; set; }
        public string AgencyPhotoURL { get; set; }
        public string AgentName { get; set; }
        public string AgentContactNo { get; set; }
        public string AgentEmail { get; set; }
        public DateTime? ImportedDateTime { get; set; }
        public int? CategoryID { get; set; }
        public int? UserID { get; set; }
        public string Suburb { get; set; }
        public string State { get; set; }
        public string PostCode { get; set; }

        [ForeignKey("CategoryID")]
        public CategoryType Category { get; set; }

         [ForeignKey("AgencyID")]
        public Actual_AgencyProfile agency { get; set; }

         [ForeignKey("UserID")]
         public UserProfile userProfile { get; set; }
    }

控制器:

 [HttpPost]
        public ActionResult Create([Bind(Exclude = "ID")]Actual_CategoryItem actual_categoryitem)
        {
            if (ModelState.IsValid)
            { 
                db.ItemList.Add(actual_categoryitem);

                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CategoryID = new SelectList(db.CategoryTypes, "CategoryID", "TypeName", actual_categoryitem.CategoryID);
            ViewBag.AgencyID = new SelectList(db.Actual_AgencyProfile, "ID", "UniqueURL", actual_categoryitem.AgencyID);
            return View(actual_categoryitem);
        }

查看:

@using (Html.BeginForm())
{
    @Html.ValidationSummary(false)
   @Html.HiddenFor(model => model.ID)
    <div class="left-col">
        <div class="clearfix"></div>
        <br />


        <div class="column-a col-md-12 col-sm-12 col-xs-12" style="padding-top: 0px;">
            <div>
                @Html.ActionLink("Back to List", "Index")
            </div>
            <div style="height: auto">
                <section class="widget">
                    <h4 class="widget-title">Add New Post</h4>


                    <div class="kopa-one-two">
                        <section class="widget">

                            <div class="entry-item">
                                <div class="col-md-12"></div>
                            </div>

                            <div id="divAgency">
                                <div class="entry-item">
                                    <div class="col-md-12">Category : </div>
                                </div>
                                <div class="entry-item">
                                    <div class="col-md-12">
                                        @Html.DropDownList("CategoryID", (IEnumerable<SelectListItem>)ViewBag.CategoryID, new { @class = "form-control" })
                                        @Html.ValidationMessageFor(model => model.CategoryID)
                                    </div>
                                </div>
                                <div class="clearfix"></div>
                            </div>

                            <div class="entry-item">
                                <div class="col-md-12">Business Name: </div>
                            </div>
                            <div class="entry-item">
                                <div class="col-md-12">
                                    @Html.TextBoxFor(o => o.Name, new { @class = "form-control" })
                                </div>
                            </div>
                            <div class="clearfix"></div>

                            <div class="entry-item">
                                <div class="col-md-12">Sale Price : </div>
                            </div>
                            <div class="entry-item">
                                <div class="col-md-12">
                                    @Html.TextBoxFor(o => o.Price, new { @class = "form-control" })
                                </div>
                            </div>
                            <div class="clearfix"></div>

                            <div class="entry-item">
                                <div class="col-md-12">Street Address : </div>
                            </div>
                            <div class="entry-item">
                                <div class="col-md-12">
                                    @Html.TextBoxFor(o => o.Location, new { @class = "form-control" })
                                </div>
                            </div>
                            <div class="clearfix"></div>

                            <div class="entry-item">
                                <div class="col-md-12">State: </div>
                            </div>
                            <div class="entry-item">
                                <div class="col-md-12">
                                    @Html.DropDownListFor(o => o.State, new List<SelectListItem>() { ( new SelectListItem() { Text = "New South Wales", Value = "NSW" }),( new SelectListItem() { Text = "Victoria", Value = "VIC" }),
                               ( new SelectListItem() { Text = "Queensland", Value = "QLD" }),( new SelectListItem() { Text = "Western Australia", Value = "WA" })
                            ,( new SelectListItem() { Text = "ACT", Value = "ACT" }),( new SelectListItem() { Text = "South Australia", Value = "SA" })
                            ,( new SelectListItem() { Text = "Tasmania", Value = "TAS" }),( new SelectListItem() { Text = "Northern Territory", Value = "NT" })}
                               , "Please Select", new { @class = "form-control" })

                                </div>
                            </div>
                            <div class="clearfix"></div>


                        </section>

                    </div>
                    <!--kopa-one-third-->
                    <div class="kopa-one-two last">
                        <section class="widget">


                            <div class="entry-item">
                                <div class="col-md-12">Suburb: </div>
                            </div>
                            <div class="entry-item">
                                <div class="col-md-12">
                                    @Html.TextBoxFor(o => o.Suburb, new { @class = "form-control" })

                                </div>
                            </div>
                            <div class="clearfix"></div>

                            <div class="entry-item">
                                <div class="col-md-12">Post Code: </div>
                            </div>
                            <div class="entry-item">
                                <div class="col-md-12">
                                    @Html.TextBoxFor(o => o.PostCode, new { @class = "form-control" })
                                </div>
                            </div>
                            <div class="clearfix"></div>

                            <div class="entry-item">
                                <div class="col-md-12">Contact No: </div>
                            </div>
                            <div class="entry-item">
                                <div class="col-md-12">
                                    @Html.TextBoxFor(o => o.AgentContactNo, new { @class = "form-control" })
                                </div>
                            </div>
                            <div class="clearfix"></div>

                            <div class="entry-item">
                                <div class="col-md-12">Contact Email: </div>
                            </div>
                            <div class="entry-item">
                                <div class="col-md-12">
                                    @Html.TextBoxFor(o => o.AgentEmail, new { @class = "form-control" })
                                </div>
                            </div>

                        </section>
                    </div>


                    <!--older-posts-->
                </section>
                <!--widget-->
                   <div class="clearfix"></div>
                            <br />
                            <div class="entry-item">
                                <div class="col-md-12" style="text-align: right">
                                    <input type="submit" id="submit-comment" value="Submit" class="btn btn-info" />
                                </div>
                            </div>
            </div>

        </div>
        <div style="padding-left: 30px">
            @Html.ActionLink("Back to List", "Index")
        </div>
    </div>

    <div class="right-col">
        .
    </div>
}

您在实体 ID 上使用 Bind(Exclude) 属性,这导致实体 ID 的值在 HttpPost 上为零。 我遇到了这个确切的问题,是由于忘记在 .cshtml 页面中添加隐藏 ID 输入造成的

事实证明,当您的数据库 table 上有 SQL 个触发器时,EF 会产生此错误。在触发器末尾添加以下内容解决了问题。然而,这可能不是完美的解决方案。

SELECT * FROM deleted UNION ALL
SELECT * FROM inserted;