IMPORTANT NOTES
1. Create several backups or copies of your XAF project, and its application database before going further, because database modifications are to be done.
2. The main purpose of the following instructions is to simplify the migration as we cannot guarantee that all existing objects will be converted correctly. Only common scenarios are covered, others may not work, and you would need to manually check if all data is converted correctly and update them further yourself in case of any problems.
How to convert a database when migrating from the 'User' and 'Role' classes to the 'SecuritySystemUser' and 'SecuritySystemRole' classes
Answers approved by DevExpress Support
Introduce additional property to your business classes to migrate reference property:
C#[DefaultClassOptions]
public class Class1 : BaseObject {
public Class1(Session session) : base(session) { }
public string Name { get; set; }
[Browsable(false)]
public User Owner_Old { get; set; }
public SecuritySystemUser Owner { get; set; }
}
Update database structure:
C#public override void UpdateDatabaseBeforeUpdateSchema() {
base.UpdateDatabaseBeforeUpdateSchema();
if(CurrentDBVersion < new Version("2.0.0.0")) {
ExecuteNonQueryCommand("DROP INDEX [iOwner_Class1] ON [Class1]", true);
ExecuteNonQueryCommand("ALTER TABLE [Class1] DROP CONSTRAINT [FK_Class1_Owner]", true);
ExecuteNonQueryCommand("EXECUTE sp_rename N'Class1.Owner', N'Owner_Old', 'COLUMN'", true);
}
}
Update database records:
C#public override void UpdateDatabaseAfterUpdateSchema() {
base.UpdateDatabaseAfterUpdateSchema();
if(CurrentDBVersion < new Version("2.0.0.0")) {
Dictionary<User, SecuritySystemUser> newUsers = new Dictionary<User, SecuritySystemUser>();
Dictionary<Role, SecuritySystemRole> newRoles = new Dictionary<Role, SecuritySystemRole>();
foreach(Role role in ObjectSpace.GetObjects<Role>()) {
SecuritySystemRole newRole = ObjectSpace.CreateObject<SecuritySystemRole>();
newRole.Name = role.Name;
newRole.IsAdministrative = true;
newRoles.Add(role, newRole);
}
foreach(User user in ObjectSpace.GetObjects<User>()) {
SecuritySystemUser newUser = ObjectSpace.CreateObject<SecuritySystemUser>();
newUser.UserName = user.UserName;
PropertyInfo newProperty = typeof(SecuritySystemUser).GetProperty("StoredPassword", BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic);
PropertyInfo oldProperty = typeof(User).GetProperty("StoredPassword", BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic);
newProperty.SetValue(newUser, oldProperty.GetValue(user));
foreach(Role role in user.Roles) {
newUser.Roles.Add(newRoles[role]);
}
newUsers.Add(user, newUser);
}
foreach(Class1 obj in ObjectSpace.GetObjects<Class1>()) {
obj.Owner = newUsers[obj.Owner_Old];
}
ObjectSpace.CommitChanges();
}
}
See the attached solutions for more details.