我正在尝试使用按钮进行 linq to sql 查询,但更改未提交。
这是我正在使用的代码:
using (Property_dbDataContext context = new Property_dbDataContext())
{
var user = (from a in context.User_profiles
where a.user_id == Convert.ToInt32(Request.QueryString["user_id"])
select a).FirstOrDefault();
user.email = _Email.Text;
user.first_name = _FirstName.Text;
user.last_name = _LastName.Text;
user.phone = _Phone.Text;
user.cell = _Cell.Text;
user.fax = _Fax.Text;
user.address = _Address.Text;
user.zip_code = _ZipCode.Text;
user.country = _Country.SelectedItem.Text;
context.SubmitChanges();
}
最佳答案
您的代码似乎是正确的。我认为您还缺少其他东西。所以我建议你应该尝试一些调试;
首先确保始终将您的代码包装在 null check
中;
if(user !=null)
{
user.email = _Email.Text;
user.first_name = _FirstName.Text;
user.last_name = _LastName.Text;
user.phone = _Phone.Text;
user.cell = _Cell.Text;
user.fax = _Fax.Text;
user.address = _Address.Text;
user.zip_code = _ZipCode.Text;
user.country = _Country.SelectedItem.Text;
context.User_profiles.Attach(user);
context.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
context.SubmitChanges();
}
其次,出于调试目的,写一些常量字符串值,看看你是否真的在发布这样的数据;
user.email = _Email.Text;
user.first_name = "test1";
user.last_name = _LastName.Text;
.........
context.SubmitChanges();
第三,别忘了,调试器是你最好的 friend :)
关于c# - 使用 LINQ to SQL 未提交到多个列的更改(更新),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27490449/