日期:2014-05-18  浏览次数:20472 次

Mvc 修改数据
Mvc EF 修改数据贼郁闷,老是出错
C# code
 public bool Edit(Student sd)
        {
            Student ss = new Student();
            ss.t_id = sd.t_id;
            ss.t_sname = sd.t_sname;
            ss.t_state = sd.t_state;

           ll.ApplyCurrentValues<Student>("Student",ss)
           ll.SaveChanges();
            return true;
        }

老是错误,是在是很无奈了,student里边只有三个字段,全部赋值了还是出错,出错信息为
ObjectStateManager 中找不到其键与所提供对象的键相匹配的对象。请确认所提供对象的键值与必须向其应用更改的对象的键值相匹配。
有点无奈,求指点,如果一个实体中有的字段我不想给他值怎么办啊,全给都出错,

------解决方案--------------------
C# code

using (var edm = new NorthwindEntities())
            {
                    Customers addc = edm.Customers.FirstOrDefault(cc => cc.CustomerID == "c2");
                    addc.City = "CD";
                    addc.ContactName = "cnblogs";
                    addc.Country = "CN";
                    int result = edm.SaveChanges();
                    Assert.AreEqual(result, 1);
                    Customers updatec = edm.Customers.FirstOrDefault(cc => cc.CustomerID == "c2");
                    Console.WriteLine("CustomerId={0},City={1}", updatec.CustomerID, updatec.City);
              }