在我的Windows 8 C#/XAML .NET 4.5应用程序中,我需要使用本地SQLite数据库。
因为我不是唯一一个做这个项目的人,所以数据库是由其他人创建的,我不喜欢在他的代码中编辑和/或搜索。
我需要找到一种方法来更新数据库中的现有元素,但我看不到任何更新的方法。我已经在谷歌上搜索过了,并找到了一种方法,我只是不确定我是否做得对(如果它能起作用)。
这是使用linq-2-sql?更新数据库中的项的正确方法吗?
示例:
MyDoctorModel doctor... //existing doctor, contains properties like Name, Phone, doctorId (which is corresponding to the Id of doctor in database)
dbContext //existing object of database, custom made, contains table object + additional methods
//I'm linq-2-sql and linq rookie, so I'm not sure if it's normal or not
//dbContext.DOCTOR is object of the table in the database, contains
//columns like Id(integer),NAME(string) etc...
var dbDoctor = dbContext.DOCTOR.Where(e => e.Id == doctor.DoctorId).First();
dbDoctor.NAME = doctor.Name;
....//etc, updating values for the actual ones
dbContext.SubmitChanges(); //and submiting changes这是更新表中现有项的正确方法吗?
我知道这可能很容易,这是一个基本的问题,但我找不到解释,这对我来说是令人满意的(足够简单,让我的笨脑袋明白)。
发布于 2014-03-05 22:05:57
是的,这是正确的方法,您首先通过db context找到要更新的对象,然后更改它的一些属性,然后调用相同上下文的SubmitChanges。
https://stackoverflow.com/questions/22210272
复制相似问题