使用LINQ to SQL更改(更新)未提交到多个列

时间:2014-12-15 18:16:59

标签: c# asp.net linq 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();
         }

2 个答案:

答案 0 :(得分:2)

您的代码似乎正确无误。我想你错过了别的东西。所以我建议你应该尝试一些调试;
首先确保将代码始终包含在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();

第三,不要忘记,调试器是你最好的朋友:)

答案 1 :(得分:0)

你试过这个吗?

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.User_profiles.Attach(user);
            context.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
            context.SubmitChanges();
         }