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

标签 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();
         }

最佳答案

您的代码似乎是正确的。我认为您还缺少其他东西。所以我建议你应该尝试一些调试;
首先确保始终将您的代码包装在 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/

相关文章:

c# - 我如何加载我的 ASHX CSS 处理程序?

c# - 使用 Windows 身份验证禁用对 ASP.NET 应用程序子文件夹的身份验证

asp.net - 在 ASP.NET GridView 中实现 JQuery 数据表

asp.net - 子文件夹中的 .NET 基本 href 和样式表链接

c# - 如何获取 WPF 应用程序的默认路径数据库位置?

c# - 插入时如何从表中获取自动生成的 ID?

c# - 动态或子句 Linq

c# - 如何从 jwt token 获取用户 ID?

c# - 使用多个命令在类中预定义字段

c# - 无法访问该进程,因为它正被另一个进程使用