如何通过EF6 C#更新sqlite中的记录 [英] How to Update record in sqlite by EF6 C#
本文介绍了如何通过EF6 C#更新sqlite中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是sqlite实体框架6的新手。
我正在尝试更新表中的记录,但它没有保存数据库中的更改。
我正在使用以下代码更新记录。
I am new to sqlite entity framework 6.
I am trying to update a record in table but it not saving the changes in the database.
I am using the following code to Update Record.
public int saveBill(general_bills cat)
{
using (alldbEntities db = new alldbEntities())
{
if (cat.ID > 0)
{
//db.Attach(cat);
//db.ApplyPropertyChanges("general_bills", cat);
//db.Attach(cat);
//db.AddTogeneral_bills(cat);
//db.Attach(cat);
//db.AcceptAllChanges();
db.AttachTo("general_bills", cat);
//db.Entry(cat).State = EntityState.Modified;
db.SaveChanges(true);
}
else
{
db.AddTogeneral_bills(cat);
db.SaveChanges(true);
}
return (int)cat.ID;
}
}
推荐答案
请尝试如下。
注意:我希望你的猫实体收藏品是AddTogeneral_bills
Please try is as below.
Note : I hope your cat entity collection is "AddTogeneral_bills"
public int saveBill(general_bills cat)
{
using (alldbEntities db = new alldbEntities())
{
if (cat.ID > 0)
{
var catObj = (from c in db.AddTogeneral_bills
where c.ID=cat.ID
select c).FirstOrDefault();
catObj.pro1= cat.pro1;
catObj.pro2= cat.pro2;
//put your properties
db.SaveChanges();
}
else
{
db.AddTogeneral_bills.Add(cat);
db.SaveChanges();
}
return (int)cat.ID;
}
}
这篇关于如何通过EF6 C#更新sqlite中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文