我在某些酒店應用程序上工作。我有一個ListView和一些列,一列是CheckBox列。當我點擊該複選框時,所選項目將從ListView中移除。 另外,當我點擊該CheckBox時,我調用WCF服務中的方法,這種方法效果不佳。在數據庫中,我有一個名爲「IsFinished」的布爾列表(tblStay)。點擊CheckBox後,我需要將該列設置爲「True」。現在這很奇怪:在我放置斷點並檢查一切是否正常工作之後,它實際上運行良好。字段「IsFinished」的值爲「True」,但在我的數據庫中它仍然設置爲false。然後我重新啓動應用程序並再次執行相同的操作,然後在我的數據庫中將其保存爲「True」。這並非總是如此。有時會保存正確,但在大多數情況下,它不起作用。這裏是我的代碼:Context.SaveChanges()在應用程序重新啓動之前不起作用
視圖模型:
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;
}
}
這究竟是爲什麼?
我不能在我的EF版本中使用它,所以我這樣做: var contactEntry = context.ObjectStateManager.GetObjectStateEntry(stay); contactEntry.ChangeState(System.Data.EntityState.Modified); 關於context.SaveChanges();當我試圖解決問題時,我使用了兩次,所以忘記在這裏刪除它。仍然不起作用 – Stojdza
而我並沒有在這裏添加新的記錄到我的數據庫,我只是更新已經存在的記錄。 – Stojdza
您使用哪種版本的EF?在你的代碼中沒有事務,所以當你調用'context.SaveChanges()'這個對象沒有被標記爲已更改,所以EF不會更改你的數據庫。如果你創建了一個新的實例,它可以工作(例如'context.tblStays.Add(aNewInstance);'? – glautrou