Linq 2 SQL:通过Web服务更新记录
我有商务舱。在此业务类中,有:
- 访问数据库的上下文(通过 .dbml)
- 一些方法
此业务类中的 SaveUser(User user)
方法由 Web 服务调用。此方法接收修改后的User
对象。如何通过作为参数接收的对象的值更新数据库中的记录(字段值具有预期值)?
我尝试了这个:
context.Users.Attach(user);
context.SubmitChanges();
我尝试没有最后一行,同样......数据库没有变化。
有什么想法吗?
谢谢,
更新 1
public class RightManager
{
private readonly DBDataContext dc;
public RightManager()
{
dc = new DBDataContext();
}
public User GetUser(int id)
{
User user = dc.GetTable<User>()
.Where(x => x.Id == id && x.IsEnable == true)
.SingleOrDefault<User>();
return user;
}
public void SaveUser(User user)
{
dc.Users.Attach(user);
}
}
I have a business class. In this business class there are :
- a context to access database (via .dbml)
- Some methods
A SaveUser(User user)
method from this business class is called by a webservice. This method receive the modified User
object. How can I update the record in the database by the value from the object receive as parameter (the fields value have the expected values) ?
I tried this :
context.Users.Attach(user);
context.SubmitChanges();
I tried without the last line, same... no change in the database.
Any idea ?
Thanks,
Update 1
public class RightManager
{
private readonly DBDataContext dc;
public RightManager()
{
dc = new DBDataContext();
}
public User GetUser(int id)
{
User user = dc.GetTable<User>()
.Where(x => x.Id == id && x.IsEnable == true)
.SingleOrDefault<User>();
return user;
}
public void SaveUser(User user)
{
dc.Users.Attach(user);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,您通过参数收到的对象不是 User 对象。您必须从上下文(通常通过 ID)获取一个,更新所需的属性,然后提交。
Sure, the object you recieve by argument is not User object. You have to get one from context (usually by Id), update required properties and submit then.
您可以使用 Attach 方法的重载之一。
请参阅这篇文章:http://blogs.msdn.com/b/dinesh.kulkarni/archive/2007/10/08/attach-if-you-have-something-detached.aspx
You can use one of the overload of the attach method.
See this post: http://blogs.msdn.com/b/dinesh.kulkarni/archive/2007/10/08/attach-if-you-have-something-detached.aspx