Context.SaveChanges()在应用程序重新启动之前不起作用

时间:2013-07-09 10:19:02

标签: c# wcf entity-framework mvvm

我在一些酒店应用程序上工作。我有一个ListView有一些列,一列是CheckBox列。当我单击该CheckBox时,所选项目将从ListView中删除。 此外,当我点击CheckBox时,我从WCF服务调用一个方法,该方法不能很好地工作。在数据库中,我有一个表(tblStay),其中一个名为“IsFinished”的布尔列。单击CheckBox后,我需要将该列设置为“True”。现在这很奇怪:在我设置断点并检查一切是否正常后,它确实运行良好。 “IsFinished”字段的值为“True”,但在我的数据库中仍然设置为false。然后我重新启动应用程序并再次执行相同的操作,然后在我的数据库中将其保存为“True”。情况也并非总是如此。有时它被正确保存但在大多数情况下它不能正常工作。这是我的代码:

视图模型:

    private ServiceReference1.tblStayGuest mainGuest;
    public ServiceReference1.tblStayGuest MainGuest  //bound as selected item in ListView
    {
        get
        {
            return mainGuest;
        }
        set
        {
            mainGuest = value;
            OnPropertyChanged("MainGuest");
        }
    }

private ObservableCollection<ServiceReference1.tblStayGuest> mainGuests;
    public ObservableCollection<ServiceReference1.tblStayGuest> MainGuests    //bound as items source in ListView 
    {
        get
        {
            return mainGuests;
        }
        set
        {
            mainGuests = value;
            OnPropertyChanged("MainGuests");
        }
    }


private ICommand _FinishedStay;  // this command is bound to my CheckBox column in listview
    public ICommand FinishedStay
    {
        get
        {
            if (_FinishedStay == null)
            {
                _FinishedStay = new DelegateCommand(delegate()
                {
                    try
                    {
                        ServiceReference1.Service1Client wcf = new ServiceReference1.Service1Client();
                        MainGuest.IsMainGuest = false;
                        wcf.FinishedStay(MainGuest);

                        if (MainGuest.tblStay.IsFinished == true)
                        {                                
                            MainGuests.Remove(MainGuest);
                        }
                        wcf.Close();
                    }
                    catch
                    {
                        Trace.WriteLine("working...", "MyApp");
                    }
                });
            }
            return _FinishedStay;
        }
    }

WCF:

  bool IService1.FinishedStay(tblStayGuest mainGuest)
    {
        try
        {
            context = new HotelBaseEntities();

            //tblStayGuest stGuest = (from stg in context.tblStayGuests where stg.StayGuestID == mainGuest.StayGuestID select stg).FirstOrDefault();
            tblStay stay = (from st in context.tblStays where st.StayID == mainGuest.StayID select st).FirstOrDefault();
            tblGuest guest = (from g in context.tblGuests where g.GuestID == mainGuest.GuestID select g).FirstOrDefault();
            tblBooking book = (from b in context.tblBookings where b.GuestID == mainGuest.GuestID select b).FirstOrDefault();
            tblRoom room = (from r in context.tblRooms where r.RoomID == mainGuest.tblStay.RoomID select r).FirstOrDefault();


            guest.IsCheckedOut = true;
            mainGuest.IsMainGuest = false;              
            stay.IsFinished = true;
            book.IsActive = false;
            book.IsCanceled = true;
            room.RoomStatus = false;
            context.SaveChanges();

            var contactEntry = context.ObjectStateManager.GetObjectStateEntry(stay);
            contactEntry.ChangeState(System.Data.EntityState.Modified);


            List<tblStayGuest> GuestsInRoom = (from gs in context.tblStayGuests where gs.StayID == mainGuest.StayID select gs).ToList();
            foreach (tblStayGuest stayG in GuestsInRoom)
            {
                tblGuest guestToCheck = (from gtc in context.tblGuests where gtc.GuestID == stayG.GuestID select gtc).FirstOrDefault();
                guestToCheck.IsCheckedOut = true;
                context.SaveChanges();
            }


            context.SaveChanges();
            return true;
        }
        catch (Exception e)
        {
            e.StackTrace.ToString();
            return false;
        }
    }

为什么会这样?

1 个答案:

答案 0 :(得分:0)

您是否尝试使用context.Entry(stay).State = EntityState.Added;,只使用一个context.SaveChanges();并在函数外部实现您的上下文?