结果显示:
abc 1234
abc 123456说明没有把修改同步至数据库。怎么办?string strconn="Data Source=PC-201002231828\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True;Pooling=False";
            DataContext db = new DataContext(strconn);
            Table<userinfo> table = db.GetTable<userinfo>();
            var a = from sou in table
                    where sou.username == "abc"
                    select sou;
            foreach (var sou2 in a)
            {
                sou2.userpassword = "1234";
                Console.WriteLine("{0}{1}", sou2.username, sou2.userpassword);
            }
            db.SubmitChanges();
            DataContext db1 = new DataContext(strconn);
            Table<userinfo> table1 = db1.GetTable<userinfo>();
            var a1 = from s in table1
                     where s.username=="abc"
                    select s;
            foreach (var sou3 in a1)
            {
                Console.WriteLine("{0}{1}",sou3.username,sou3.userpassword);
            }
            Console.ReadLine();

解决方案 »

  1.   

    using (TestDBEntities db = new TestDBEntities())
            {
                var dept = db.Depatment.Where(i => i.Id == 1).First();
                dept.DeptName = "AAA";
                Employee employee = new Employee() {Name = "AAA"};
                dept.Employee.Add(employee);
                db.SaveChanges();
            }
      

  2.   

    using (TestDBEntities db = new TestDBEntities())
      {
      var dept = db.Depatment.Where(i => i.Id == 1).First();
      dept.DeptName = "AAA";
      Employee employee = new Employee() {Name = "AAA"};
      dept.Employee.Add(employee);
      db.SaveChanges();
      }
    我是新手,完全看不懂。
      

  3.   

    Table<userinfo> table = db.GetTable<userinfo>();不明白你为什么这么用。直接用db.userinfo就可以了。var a = from sou in db.userinfo
            where sou.username == "abc"
            select sou;怀疑你更新的只是表数据的副本。
      

  4.   

    没具体看原因,不能通过GetTable<T>来操作DB。
    GetTable<userinfo> 改为 db.userinfohttp://stackoverflow.com/questions/4287983/working-with-datacontext-gettablet-to-get-a-queryprovider
      

  5.   

    应该是不能通过GetTable<T>来access DB,但是Query还是OK的。
      

  6.   

    db.userinfo 不存在 我试过。
    Table<userinfo> table = db.userinfo
    点不出userinfo的。
      

  7.   

    而且书上就是怎么写的。GetTable
      

  8.   

    你的.dbml没弄好,你的表没主键吧。需要主键。
      

  9.   

    SubmitChanges 这个更新也是OK的
    你那个Table其实是多此一举,查询的东西是从Table里面的,再更新到上下文
    而上下文的db又没有改变,所以
      

  10.   


    如果使用table的话,使用这种方式table.Single(sou.username == "abc");
      

  11.   

    恩。table的多次一举不理解。
    能跟我讲讲吗?
    上面说的 直接使用db.userinfo。但是db点不出userinfo。
      

  12.   

    没有userinfo是因为mdf表没有主键,做好主键删掉现有的dbml重新生成。
    就能点出userinfo了。否则你现在连Insert都没有。
      

  13.   

    为题原因找到了。这是csdn别的有心人写的叫phy
    HOHO。这个原因很简单因为楼主没有定义Customer表格的主键, LINQ没有这个信息就无法准确的更新数据库内的对象,所以把你的操作忽略了。  修改的方法是把CustomerId字段改为如下:  C# code
    private int _customerId;[Column(Storage = "_customerId", DbType = "Int NOT NULL", IsPrimaryKey = true)]http://topic.csdn.net/u/20080823/05/fc9e1a93-153d-4dcc-ad52-a62b850fabae.html
      

  14.   

    解决办法就是:
    用SQL Sever直接把username做主键,或者用其他办法。
    做了主键以后 使用O/R设计器 
    把映射好的代码复制到程序中或者直接手工写红色代码
    [Column(Storage = "username", DbType = "Int NOT NULL", IsPrimaryKey = true)]