我正在处理一些酒店应用程序。我有一个包含一些列的 ListView,其中一列是 CheckBox 列。当我单击该复选框时,所选项目将从 ListView 中删除。此外,当我单击该复选框时,我会从 WCF 服务调用一个不能很好地工作的方法。在数据库中,我有一个表(tblStay),其中包含一个名为“IsFinished”的布尔列。单击 CheckBox 后,我需要将该列设置为“True”。现在这很奇怪:在我设置断点并检查一切是否正常之后,它实际上运行良好。“IsFinished”字段的值为“True”,但在我的数据库中它仍然设置为false。然后我重新启动应用程序并再次执行相同的操作,然后在我的数据库中将其保存为“True”。此外,情况并非总是如此。有时它' s 保存正确,但在大多数情况下它不能正常工作。这是我的代码:
视图模型:
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;
}
}
周转基金:
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;
}
}
为什么会这样?