如何在自身内部保存EntityFramework模型?

时间:2016-11-17 19:00:10

标签: c# entity-framework model savechanges

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

作为一个例子,我希望我的应用程序在其数据库中更新用户的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();
}

有什么想法吗?

谢谢,

罗素

2 个答案:

答案 0 :(得分:0)

您的类中的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();
        }
}

答案 1 :(得分:0)

如果有其他人遇到此问题 - 我可以通过在用户内创建用户来保存User Partial类中的用户:

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

我希望这对某人有帮助。 : - )

罗素