C# 如何将项目更新发送到DotNetNuke模块中的数据库?
我是DotNetNuke的新手,我正在创建一个新模块,我正在尝试找出在数据库中上传数据的最佳方式 我使用了以下代码:C# 如何将项目更新发送到DotNetNuke模块中的数据库?,c#,sql,dotnetnuke,C#,Sql,Dotnetnuke,我是DotNetNuke的新手,我正在创建一个新模块,我正在尝试找出在数据库中上传数据的最佳方式 我使用了以下代码: public void UpdateStudentByID(int studentID, Student student) { using (IDataContext ctx = DataContext.Instance()) { var rep = ctx.GetRepository<Student>(); rep.U
public void UpdateStudentByID(int studentID, Student student)
{
using (IDataContext ctx = DataContext.Instance())
{
var rep = ctx.GetRepository<Student>();
rep.Update(student);
}
}
从查询中可以明显看出,没有生成用于更新特定项的where条件
那么,将对象更新到数据库中的最佳方法是什么呢?您传入的学生对象没有将其StudentId属性设置为值——这就是为什么where条件尝试更新where StudentId=0的原因。在调用函数之前设置该值,您就可以开始了 通过这样做,您不需要通过studentId和student调用UpdateStudentByID,而只需要调用UpdateStudent和passstudent
...
//Before calling UpdateStudent, make sure your StudentId property is set
student.StudentId = 123;
....
public void UpdateStudent(Student student)
{
using (IDataContext ctx = DataContext.Instance())
{
var rep = ctx.GetRepository<Student>();
rep.Update(student);
}
}
。。。
//在调用UpdateStudent之前,请确保设置了StudentId属性
student.StudentId=123;
....
公共无效更新学生(学生)
{
使用(IDataContext ctx=DataContext.Instance())
{
var rep=ctx.GetRepository();
代表更新(学生);
}
}
希望这有帮助
...
//Before calling UpdateStudent, make sure your StudentId property is set
student.StudentId = 123;
....
public void UpdateStudent(Student student)
{
using (IDataContext ctx = DataContext.Instance())
{
var rep = ctx.GetRepository<Student>();
rep.Update(student);
}
}