【问题标题】:Context.SaveChanges() Doesn't work until application restartsContext.SaveChanges() 在应用程序重新启动之前不起作用
【发布时间】:2013-07-09 10:19:02
【问题描述】:

我正在处理一些酒店应用程序。我有一个 ListView 有一些列,一列是 CheckBox 列。当我单击该复选框时,所选项目将从 ListView 中删除。 此外,当我单击该复选框时,我会从 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;
        }
    }

为什么会这样?

【问题讨论】:

    标签: c# wcf entity-framework mvvm


    【解决方案1】:

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

    【讨论】:

    • 我不能在我的 EF 版本中使用它,所以我这样做了: var contactEntry = context.ObjectStateManager.GetObjectStateEntry(stay); contactEntry.ChangeState(System.Data.EntityState.Modified);关于 context.SaveChanges();当我试图解决问题时,我使用了它两次,所以忘了在这里删除它。还是不行
    • 我并没有在我的数据库中添加新记录,我只是更新已经存在的记录。
    • 您使用的是哪个版本的 EF?您的代码中没有事务,因此当您调用 context.SaveChanges() 时,该对象似乎未标记为已更改,因此 EF 不会更改您的数据库。如果您创建一个新实例,它是否可以工作(例如context.tblStays.Add(aNewInstance);?
    猜你喜欢
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多