在ObjectStateManager中找不到具有与提供的对象的键匹配的键的对象

时间:2012-09-14 12:14:09

标签: asp.net-mvc entity-framework

我使用以下代码

Tel_CallHistory CallHistory = new Tel_CallHistory();


                     CallHistory.MobileNo = MobileNo;
                     CallHistory.CRId = CallResponse;
                     CallHistory.CallRemark = CallRemark;
                     CallHistory.UserId = UserId;
                     CallHistory.CallDate = DateTime.Now;
                     CallHistory.CallTime = DateTime.Now.TimeOfDay;

                     CallHistory.MeetingPoint = meetingpoint;
                     if (CallResponse == 1)
                     {
                         CallHistory.NCallDateOn = null;
                         CallHistory.NCallTimeAt = null;
                         CallHistory.MeetingDate = Convert.ToDateTime( meetingdate);

                         CallHistory.MeetingTime =  DateTime.Parse(meetingtime).TimeOfDay;
                     }
                     else
                     {

                         CallHistory.NCallDateOn =  Convert.ToDateTime(NCallDateOn);

                         CallHistory.NCallTimeAt =  DateTime.Parse(NCallTimeAt).TimeOfDay;  

                         CallHistory.MeetingDate = null;
                         CallHistory.MeetingTime = null;
                     }


                     db.Tel_CallHistory.AddObject(CallHistory);


                     Tel_Mobile TelMobile = _teacher.getDB().Tel_Mobile.Single(a => a.MobileNo == MobileNo);
                     TelMobile.CallDate = CallHistory.NCallDateOn;
                     TelMobile.CallTime = CallHistory.NCallTimeAt;

                     TelMobile.LCallDateOn = CallHistory.CallDate;
                     TelMobile.LCallTimeAt = CallHistory.CallTime;
                     TelMobile.LCRId = CallHistory.CRId;
                     TelMobile.LCallRemark = CallHistory.CallRemark;
                     TelMobile.LMeetingPoint = CallHistory.MeetingPoint;
                     TelMobile.LMeetingDate = CallHistory.MeetingDate;
                     TelMobile.LMeetingTime = CallHistory.MeetingTime;

                      db.Tel_Mobile.ApplyOriginalValues(TelMobile);


                     db.SaveChanges();



                     db.AcceptAllChanges();

但是当db.Tel_Mobile.ApplyOriginalValues(TelMobile)执行时,显示错误

  

具有与所提供对象的键匹配的键的对象   在ObjectStateManager中找不到。验证该对象   必须应用更改的状态不是已添加状态   其键值与提供的对象的键值匹配。

1 个答案:

答案 0 :(得分:0)

您可能想要附上它。

db.Tel_Mobile.Attach(TelMobile);