gpt4 book ai didi

c# - db.SaveChanges 不提交更改

转载 作者:行者123 更新时间:2023-11-30 17:27:36 25 4
gpt4 key购买 nike

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

我的 Controller :

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 />

}

最佳答案

您的代码看起来没问题。尝试另一种方法来插入数据:

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 文件的属性为 Copy to output Directory: "Copy only if newer",否则您的 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 ),
);

关于c# - db.SaveChanges 不提交更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54664506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com