c# – 使用LINQ to SQL将更改(更新)提交到多个列

我正在使用按钮尝试
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();
}

其次,为了调试目的,写一些const字符串值,看看你是否真的发布这样的数据;

user.email = _Email.Text;
            user.first_name = "test1";
            user.last_name = _LastName.Text;
           .........
            context.SubmitChanges();

第三,别忘了,调试器是你最好的朋友:)

点赞