db.SaveChanges不提交更改

时间:2019-02-13 07:13:45

标签: c# sql-server asp.net-mvc

我已经与ado.net实体模型建立了联系。我可以使用下拉列表从数据库中检索信息,但是无法将更改(插入)提交到数据库中。以前两天前就可以正常运行,但是今天就停止工作。我尝试了类似主题的所有技巧,但没有任何变化。

我的Controlller:

public class RimySaleController : Controller
{
    // GET: RimySale
    // dropdownlist

    public ActionResult RimSaleIndex()
    {

        newLayanDBEntities17 db = new newLayanDBEntities17();
        List<Rim> list = db.Rims.ToList();
        ViewBag.RimName = new SelectList(list, "rim_id", "rim_name");

        List<Employee> lists = db.Employees.ToList();
        ViewBag.EmpName = new SelectList(lists, "emp_id", "emp_name");

        List<Customer> listp = db.Customers.ToList();
        ViewBag.CustName = new SelectList(listp, "cust_id", "cust_name");

        return View();
    }

    public ActionResult RimSaleSave(RimSale model)
    {
        try
        {
            newLayanDBEntities17 db = new newLayanDBEntities17();
            Rim_Sale addRim = new Rim_Sale();

            addRim.Date = model.Date;
            addRim.cust_int = model.Cust_int;
            addRim.emp_id = model.Emp_id;
            addRim.rim_id = model.Rim_id;
            addRim.rim_sold_quantity = model.Rim_sold_quantity;
            addRim.rim_sold_unit_price = model.Rim_sold_unit_price;

            //making the changes
            db.Rim_Sale.Add(addRim);
            db.SaveChanges();
         }

        catch (Exception ex)
        {
            throw ex;

        }

        return RedirectToAction("RimSaleIndex");
    }

    }
}

我的观点:

@using (Html.BeginForm("SaveBattSale", "BatterySale", FormMethod.Post))
{
            <br />
            @Html.TextBoxFor(model => model.Sold_date, new { @type = "Date", @class = "form-control" })
            <br />
            @Html.DropDownListFor(model => model.Bat_id, ViewBag.BattName as SelectList, "--Select Battery--", new { @class = "form-control" })
            <br />
            @Html.TextBoxFor(model => model.Batt_sold_quantity, new { @type = "number", @placeholder = "Type Quantity Sold", @class = "form-control " })
            <br />
            @Html.TextBoxFor(model => model.Batt_unit_price, new { @placeholder = "Price Paid for One Battery", @type = "number", @class = "form-control" })
            <br />
            @Html.DropDownListFor(model => model.Cust_id, ViewBag.CustName as SelectList, "--Select Customer--", new { @class = "form-control" })
            <br />
            @Html.DropDownListFor(model => model.Emp_id, ViewBag.EmpName as SelectList, "--Select Employee--", new { @class = "form-control" })
            <br />
            <input type="Submit" value=" Submit" />  <input type="reset" value=" Reset" />
            <br />

 }

1 个答案:

答案 0 :(得分:1)

您的代码看起来还不错。尝试另一种插入数据的方法:

using (var db = new YourEntities()) 
{ 
    Rim_Sale addRim = new Rim_Sale();

    addRim.Date = model.Date;
    addRim.cust_int = model.Cust_int;
    addRim.emp_id = model.Emp_id;
    addRim.rim_id = model.Rim_id;
    addRim.rim_sold_quantity = model.Rim_sold_quantity;
    addRim.rim_sold_unit_price = model.Rim_sold_unit_price;

    db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Rim_Sale] ON");
    //making the changes
    db.Rim_Sale.Add(addRim);
    db.SaveChanges()
    db.Entry(addRim).State = EntityState.Added;
    db.SaveChanges(); 
    db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Rim_Sale] OFF");
}

如果未插入数据,则可能会将数据插入到另一个数据库。如果使用localDb,请确保解决方案中.mdf文件的属性为“复制到输出目录”:“仅在更新时复制”,否则您的db文件每次运行时都会被覆盖。

更新:

这不是一个好方法,但是请尝试一下:

db.Database.ExecuteSqlCommand(
   "Insert into Rim_Sale Values(@Date, @cust_int, @emp_id, @rim_id, 
        @rim_sold_quantity , @rim_sold_unit_price )",
    new SqlParameter("Date", Date),
    new SqlParameter("cust_int", cust_int),
    new SqlParameter("emp_id", emp_id),
    new SqlParameter("rim_id", rim_id),
    new SqlParameter("rim_sold_quantity ", rim_sold_quantity ),
    new SqlParameter("rim_sold_unit_price ", rim_sold_unit_price ),
);