chamara
chamara

Reputation: 12709

insert statement affected an unexpected number of rows (0)

I'm trying to insert a record using Entity Framework Code First. When trying to save the record I'm Keep getting the following error.

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.

Model:

 [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; }
    }

Controller:

 [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);
        }

View:

@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>
}

Upvotes: 0

Views: 2082

Answers (2)

chamara
chamara

Reputation: 12709

Turn out that EF produces this error when you have SQL triggers on your database table. Adding following at the end of the trigger solved the problem. However this may not be the perfect solution.

SELECT * FROM deleted UNION ALL
SELECT * FROM inserted;

Upvotes: 1

stylishCoder
stylishCoder

Reputation: 385

you are using the Bind(Exclude) attribute on the entity ID which was causing the value of the entity's ID to be zero on HttpPost. I had this exact issue, caused by forgetting to add the hidden ID input in the .cshtml page

Upvotes: 0

Related Questions