我如何保存实体框架模型本身

本文关键字:框架 实体 模型 保存 何保存 | 更新日期: 2023-09-27 18:18:23

我有一个名为User的模型(来自我的数据库),我正在部分类中添加方法,允许User对象执行维护类型的工作。

作为一个例子,我希望我的应用程序更新用户的superorid在其数据库与另一个数据库对齐:

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();
}

任何想法?

谢谢,罗素

我如何保存实体框架模型本身

类中的db对象与调用此方法的程序中的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();
        }
}

以防其他人遇到这种情况-我能够在User Partial类中保存一个User,通过在User中创建一个User:

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();
        }
}
我希望这对某人有帮助。: -) 罗素