Ticket T340154
Visible to All Users

How to convert a database when migrating from the 'User' and 'Role' classes to the 'SecuritySystemUser' and 'SecuritySystemRole' classes

created 9 years ago (modified 8 years ago)

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.

Answers approved by DevExpress Support

created 9 years ago (modified 8 years ago)

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.

See Also:
How to use the Allow/Deny permissions policy (aka the PermissionPolicyUser and PermissionPolicyRole classes) in the existing project

    Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

    Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.