从WCF服务保存对数据库的更改不起作用

时间:2013-06-26 14:20:52

标签: c# wpf wcf mvvm

我在一些酒店应用程序上工作。我在主窗口中有一个列表视图和酒店住宿列表。 Listview有一个复选框列,用于通知天气是否已完成停留。 Listview也从视图模型绑定到属性。复选框绑定到视图模型中的命令。

这是listview

的绑定
<ListView SelectionMode="Single" Height="428" HorizontalAlignment="Left" Margin="6,6,0,0" Name="lvStays" VerticalAlignment="Top" Width="654" BorderBrush="Black" Foreground="Black" ItemsSource="{Binding Path=MainGuests}" SelectedItem="{Binding Path= MainGuest}" />

MainWindowViewModel

 private ServiceReference1.tblStayGuest mainGuest;
    public ServiceReference1.tblStayGuest MainGuest
    {
        get
        {
            return mainGuest;
        }
        set
        {
            mainGuest = value;
            OnPropertyChanged("MainGuest");
        }
    }

    private ObservableCollection<ServiceReference1.tblStayGuest> mainGuests;
    public ObservableCollection<ServiceReference1.tblStayGuest> MainGuests     
    {
        get
        {
            return mainGuests;
        }
        set
        {
            mainGuests = value;
            OnPropertyChanged("MainGuests");
        }
    }

private ICommand _FinishedStay;
    public ICommand FinishedStay
    {
        get
        {
            if (_FinishedStay == null)
            {
                _FinishedStay = new DelegateCommand(delegate()
                {
                    try
                    {
                        ServiceReference1.Service1Client wcf = new ServiceReference1.Service1Client();
                        wcf.FinishedStay(MainGuest);

                        if (MainGuest.tblStay.IsFinished == true)
                        {

                            MainGuests.Remove(MainGuest);
                        }                            
                    }
                    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();

            guest.IsCheckedOut = true;
            stGuest.IsMainGuest = false;
            stay.IsFinished = true;
            context.SaveChanges();

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

我在这里要管理的是将 mainGuest.IsMainGuest 的值更改为 false 。它是在wcf方法上的值更改但是在FinishedStay命令中我试图这样做:MainGuests.Remove(MainGuest); MainGuest.IsMainGuest的值恢复为True。此值也不会在数据库表中更改。 为什么会这样?

0 个答案:

没有答案