2016-11-17 27 views
0

我有一個模型,稱爲用戶(從我的數據庫派生),我在部分類中添加方法,允許用戶對象執行維護類型的工作。如何將EntityFramework模型保存在其內部?

舉個例子,我想我的應用程序與其他數據庫更新用戶的SupervisorID在其數據庫中比對:

public partial class User 
{ 
    DatabaseEntities db = new DatabaseEntities(); 
    OtherEntities otherDB = new CommonEntities(); 

     public void UpdateSupervisor() 
     { 
      // Lookup the Supervisor's Username in the other Database: 
      string supervisorUsername = (from x in otherDB.vwEmployees 
           join y in otherDB.vwEmployees on x.Supervisor_EID equals y.Employee_ID 
           where x.User_ID == searchUsername 
           select y.User_ID).FirstOrDefault(); 

      // Update the database to record the Supervisor of this staff member: 
      this.SupervisorUserID = (from x in db.Users 
            where x.ADUsername == supervisorUsername 
            select x.UserID).FirstOrDefault(); 

      // At this point, this.SupervisorUserID stores the correct int, but the following command doesn't save any changes. 
      db.SaveChanges(); 
     } 
} 

代碼工作,除了db.SaveChanges()不保存任何事情(沒有錯誤)。

的方法被稱爲這個類的外部,作爲登錄處理的一部分:

public class Login { 
// Connect to the database: 
DatabaseEntities db = new DatabaseEntities(); 

User currentUser = new User(); 

// Do some stuff... 

// Update the user's supervisor: 
currentUser.UpdateSupervisor(); 
} 

任何想法?

感謝,

羅素

+0

是'DatabaseEntities'與'db'一樣的''實例嗎?否則,它不會檢測到任何更改,因爲'db'僅用於在您發佈的代碼中獲取數據。如果使用另一個實例接收到'this',它將不起作用。 –

+0

更新了問題 - 我希望它更清楚。 :-) –

回答

0

你的類裏db對象是從一個在你的程序是調用此方法不同。另外,您的用戶對象可能不存在於數據庫中。所以,它不像設置屬性然後保存那麼簡單。

你將不得不在你的類裏使用你的db對象,然後相應地進行更新或添加。

public partial class User 
{ 
    DatabaseEntities db = new DatabaseEntities(); 
    OtherEntities otherDB = new CommonEntities(); 

     public void UpdateSupervisor() 
     { 
      // Lookup the Supervisor's Username in the other Database: 
      string supervisorUsername = (from x in otherDB.vwEmployees 
             join y in otherDB.vwEmployees on x.Supervisor_EID equals y.Employee_ID 
             where x.User_ID == searchUsername 
             select y.User_ID).FirstOrDefault(); 

      // Update the database to record the Supervisor of this staff member 
      this.SupervisorUserID = (from x in db.Users 
            where x.ADUsername == supervisorUsername 
            select x.UserID).FirstOrDefault(); 

      // Find user in DBContext using your primary key... 
      var user = db.Users.Find(this.ID); 
      if (user != null) 
      { 
       // User exists in database, update id 
       user.SupervisorUserID = this.SupervisorUserID; 
      } 
      else 
      { 
       // User does not exist in database 
       db.Users.Add(this); 
      } 
      db.SaveChanges(); 
     } 
} 
+0

更新了問題以提供幫助 - 該方法位於User類中,該類是與EF從我的數據庫創建的User類匹配的部分類。在類內部,我不創建用戶實例,因爲我已經在類中 - 因此是「this」語法。我希望有所幫助。 –

+0

即使如此,我相信用戶類中的上下文與您在其他方法中創建對象時可能使用的上下文不同。 – BlueMeanie

+0

也許我不清楚上下文和實體之間的區別......我再次更新了代碼 - 兩個類都使用DatabaseEntities db = new DatabaseEntities();連接到數據庫。我沒有任何包含「上下文」一詞的代碼。 :-) 感謝您的幫助。 –

0

萬一別人遇到這一點 - 我能夠保存用戶部分類中的一個用戶,通過創建用戶內部用戶:

public partial class User 
{ 
    DatabaseEntities db = new DatabaseEntities(); 
    OtherEntities otherDB = new CommonEntities(); 

     public void UpdateSupervisor() 
     { 
      // Lookup the Supervisor's Username in the other Database: 
      string supervisorUsername = (from x in otherDB.vwEmployees 
           join y in otherDB.vwEmployees on x.Supervisor_EID equals y.Employee_ID 
           where x.User_ID == searchUsername 
           select y.User_ID).FirstOrDefault(); 

      // Update the database to record the Supervisor of this staff member: 
      this.SupervisorUserID = (from x in db.Users 
            where x.ADUsername == supervisorUsername 
            select x.UserID).FirstOrDefault(); 
      // NEW CODE: 
      // Create a new user inside the User class, set to the current User by ID: 
      User tempUser = db.Users.Find(this.UserID); 
      tempUser.SupervisorUserID = this.SupervisorUserID; 
      db.SaveChanges(); 
     } 
} 

我希望這是有幫助的人。 :-)

Russell

相關問題