2010-10-18 39 views
1

我的基於WPF桌面的應用程序使用ADO.Net Entity Framework來連接到SQL Server數據庫。在其中一個窗口中,我有一個包含tbl_users所有數據的DataGrid,當用戶選擇其中一行(記錄)並單擊Edit時,應用程序將打開一個帶窗體的新窗口,其中包含用戶可以編輯/更新的所有數據。如何通過ADO.NET實體框架從數據庫更新記錄?

我的問題是如何通過ADO.NET實體框架將表中某個值的更改(更新/編輯)保存到數據庫?

這裏有一些代碼段,有助於瞭解情況:

1 - 編輯窗口構造

public object objToBeEdited; 

public WinWorkers_EditWorker(tbl_users userToBeEdited) 
{ 
    this.Title = Glidus.Properties.Resources.WinWorkers_EditWorker_WinName + Glidus.Properties.Resources.WinApp_WinName; 
} 

2 - 方法更新,不工作

tbl_users newUser = new tbl_users() //assume inputted values to new ibject 
{ 
    userName = this.WinWorkers_AddEditWorkers_Form_UserName.Text, 
    userPassword = this.WinWorkers_AddEditWorkers_Form_Password.Text, 
    userAccessLevel = this.WinWorkers_AddEditWorkers_Form_UserAccessLevel.Text 
}; 

//default minimal password length is 4 
if (App.IsInputValueMinLenOK(newUser.userPassword, 4)) 
{ 
    EntityKey key = App.glidusContext.CreateEntityKey("tbl_users", objToBeEdited); 

    if (App.glidusContext.TryGetObjectByKey(key, out objToBeEdited)) 
    { 
     App.glidusContext.ApplyCurrentValues<tbl_users>(key.EntitySetName, newUser); 
    } 

    try 
    { 
     App.glidusContext.SaveChanges(); 
    } 
    catch (Exception ex) 
    { 
     App.UnitedHandleException(ex); 
    } 

請,幫助我,如何從數據庫實現更新ADO.NET記錄。

例如,在我的桌子上有007,後編輯頁面(點擊提交頁)我看王牌大賤諜008

回答

2

修改或更新,你可以使用stub entities

Category category = new Category { ID = 5}; 
Context.AttachTo(「Categories」,category); 

Product product = new Product { 
    Name = 「Bovril」, 
    Category = category 
}; 
Context.AddToProducts(product); 
Context.SaveChanges();