• How to: Handle Renamings and Deletions of Business Classes and their Properties 如何:处理业务类及其属性的重命名和删除


    When developing an XAF application, you may be required to rename a persistent class or property due to refactoring specifics or changed business requirements. An XAF application launched in debug mode automatically creates required tables and columns in a database after adding new classes or properties (see the Business Classes vs Database Tables topic). However, when you rename a persistent class that already has a corresponding table in a database, this class is treated as new, and a new table is created. As a result, the old table remains unused and renamed class data becomes unavailable. The same holds true when you rename a persistent property that already has a corresponding column in a database table. A new column is created for the new property and the old column remains unused. At the development stage, these are not big problems - you can manually rename a required table/column or even create a new database. But this approach is not suitable when your application is already distributed to end-users, and they have databases with production data. This topic describes a way of automatically handling database structure changes when updating an XAF application, and avoiding the manual updating of all end-user databases. Several typical scenarios are provided, and you will either follow one of them or combine them to handle more complex changes:

    开发 XAF 应用程序时,由于重构细节或更改业务需求,您可能需要重命名持久性类或属性。在调试模式下启动的 XAF 应用程序在添加新类或属性后自动在数据库中创建所需的表和列(请参阅业务类与数据库表主题)。但是,重命名数据库中已具有相应表的持久类时,此类将被视为新类,并创建一个新表。因此,旧表保持未使用状态,重命名的类数据将不可用。重命名已在数据库表中具有相应列的持久属性时也是如此。为新属性创建一个新列,并且旧列保持未使用状态。在开发阶段,这些问题并不大 - 您可以手动重命名所需的表/列,甚至创建新数据库。但是,当应用程序已分发给最终用户,并且具有包含生产数据的数据库时,此方法并不适合。本主题介绍一种在更新 XAF 应用程序时自动处理数据库结构更改的方法,并避免手动更新所有最终用户数据库。提供了几种典型的方案,您将遵循其中一种方案或组合它们来处理更复杂的更改:

    • Rename the Persistent Property
    • Remove the Persistent Property
    • Change the Persistent Property's Data Type
    • Rename the Persistent Class
    • Rename the Persistent Class Participant in a Many-to-Many Relationship
    • Rename the Persistent Class Used as the Data Type in Analysis
    • Rename the Persistent Class Used as the Data Type in Reports
    • Remove the Persistent Class
    • 重命名持久属性
    • 删除持久属性
    • 更改持久属性的数据类型
    • 重命名持久类
    • 在多对多关系中重命名持久类参与者
    • 重命名用作分析中数据类型的持久类
    • 重命名用作报表中数据类型的持久类
    • 删除持久类
    Important 重要
    Some of the ModuleUpdater class' protected methods, like RenameTable and RenameColumn demonstrated in this topic, are applicable when you are directly connected to MS SQL Server, and may not work with different database engines. If you run into an exception when using these methods, you can pass an appropriate query to the ExecuteNonQueryCommand, ExecuteScalarCommand or ExecuteReader commands instead.
    当您直接连接到 MS SQL Server 时,某些 ModuleUpdater 类的受保护方法(如本主题中演示的重命名表和重命名列)适用于直接连接到 MS SQL Server,并且可能不适用于不同的数据库引擎。如果在使用这些方法时遇到异常,则可以将适当的查询传递给"执行非查询命令"、"执行Scalar命令"或"执行读取器"命令。

    Rename the Persistent Property

    重命名持久属性

    Lets assume you have the Department class with the Office property, and it is required to rename this property to Room.

    假设您具有 Office 属性的部门类,并且需要将此属性重命名为"房间"。

    [DefaultClassOptions ]
    public class Department : BaseObject {
        // ...
        private string office;
        public string Office {
            get { return office; }
            set { SetPropertyValue(nameof(Office), ref office, value); }
        }
        // ...

    The following steps are required to rename a property, and handle the database and application model changes.

    需要以下步骤来重命名属性,并处理数据库和应用程序模型更改。

    • rename the property in C# or VB sources
    • rename the property in XAFML and BO files
    • rename the table column holding the renamed property values
    • 在 C# 或 VB 源中重命名属性
    • 重命名 XAFML 和 BO 文件中的属性
    • 重命名保留重命名属性值的表列

    These steps are detailed below.

    这些步骤将在下面详细说明。

    • Rename the Property in C# or VB sources
    • 在 C# 或 VB 源中重命名属性

    Use the Visual Studio Refactor | Rename... command to look through your solution sources and rename the property anywhere it is used.

    使用可视化工作室重构 |重 命名。。。命令查看解决方案源,并在使用任何位置重命名属性。

    HowToHandleDbChanges1

    Rebuild the solution to ensure that it is compilable, but do not run the application to avoid the creation of a new table column for the new property name.

    重新生成解决方案以确保它是可编译的,但不要运行应用程序以避免为新属性名称创建新表列。

    • Rename the Property in XAFML and BO Files

    • 重命名 XAFML 和 BO 文件中的属性

    Refactoring tools do not update XAFML code. Use the Find and Replace dialog to look through XAFML files to rename a property. This dialog is available in Visual Studio using the Edit | Find and Replace | Quick Replace menu command or the CTRL-H shortcut. For instance, this may be required if you have a customized Department Detail View layout, or if the Office property is used in the filter criteria. Otherwise, the customizations will be lost.

    重构工具不会更新 XAFML 代码。使用"查找和替换"对话框查看 XAFML 文件以重命名属性。此对话框在可视化工作室中使用编辑 |查找和替换 |快速替换菜单命令或 CTRL-H 快捷方式。例如,如果您有自定义的"部门详细信息视图"布局,或者在筛选条件中使用 Office 属性,则可能需要这样做。否则,自定义项将丢失。

    HowToHandleDbChanges11

    • Rename the Table Column Holding the Renamed Property Values

    • 重命名保留重命名属性值的表列

    The Office column in the database table storing the Office property values should be renamed before updating database schema.

    在更新数据库架构之前,应重命名数据库表中存储 Office 属性值的 Office 列。

    HowToHandleDbChanges5

    The ModuleUpdater.UpdateDatabaseBeforeUpdateSchema method is intended to update an application database before the database schema is updated. We will override this method to perform the required changes with the database structure. The ModuleUpdater class exposes the RenameColumn protected method, which renames the required column in the database table. The following snippet illustrates how to rename the Office column in the Department table to Room.

    模块更新器.Update 数据库之前更新架构方法用于在更新数据库架构之前更新应用程序数据库。我们将重写此方法以使用数据库结构执行所需的更改。模块更新器类公开重命名列受保护方法,该方法重命名数据库表中所需的列。以下代码段演示如何将"部门"表中的"Office"列重命名为"会议室"。

    public class Updater : ModuleUpdater {
        // ...
        public override void UpdateDatabaseBeforeUpdateSchema() {
            base.UpdateDatabaseBeforeUpdateSchema();
            if (CurrentDBVersion < new Version("1.1.0.0") 
                && CurrentDBVersion > new Version("0.0.0.0")) {
                RenameColumn("Department", "Office", "Room");
            }
         }
        // ...

    The "1.1.0.0" string is the application version in which changes are introduced. So, the column will be renamed only if the database version is less than "1.1.0.0". It is required to check if the database version is greater than "0.0.0.0", to make changes only when the database exists and is filled with data (the empty database version is "0.0.0.0", by default).

    "1.1.0.0"字符串是引入更改的应用程序版本。因此,仅当数据库版本小于"1.1.0.0"时,才会重命名该列。需要检查数据库版本是否大于"0.0.0.0",仅在数据库存在且充满数据时进行更改(默认情况下,空数据库版本为"0.0.0.0")。

    The SQL command actually executed within this example is:

    此示例中实际执行的 SQL 命令是:

    EXECUTE sp_rename N'Department.Office', N'Room"', 'COLUMN'

    The SQL command text is written to the application Log File with the "ExecuteNonQueryCommand:" prefix before being executed. If an error occurs while executing the RenameColumn method, the exception text is also logged. However, the application execution is not interrupted in the case of an error. You can refer to the application log file for debugging purposes.

    SQL 命令文本使用"执行NonQuery命令:"写入应用程序日志文件。执行前缀。如果在执行重命名列方法时发生错误,也会记录异常文本。但是,在出现错误时,应用程序执行不会中断。出于调试目的,可以参考应用程序日志文件。

    • Increment the application version, run the application and ensure that the property name changed and that data is available.
    • 增加应用程序版本,运行应用程序并确保属性名称已更改且数据可用。

    You can check that the column name was modified during the database update. If your database server is Microsoft SQL Server, run Microsoft SQL Management Studio and navigate to the modified column using the Object Explorer.

    可以检查列名称在数据库更新期间是否被修改。如果数据库服务器是 Microsoft SQL Server,请运行 Microsoft SQL 管理工作室,并使用对象资源管理器导航到修改后的列。

    HowToHandleDbChanges4

    If you use another database server, use the appropriate database tool to check if the column name was actually modified.

    如果使用其他数据库服务器,请使用相应的数据库工具检查列名称是否实际修改。

    Remove the Persistent Property

    删除持久属性

    If you remove the persistent property from the application code, it will not be visible in the application interface. But, the corresponding column will still exist in the application database. To remove a column, use the DropColumn protected method, exposed by the ModuleUpdater class. This method removes the specified column on the specified table. The following snippet illustrates how to remove the Room column from the Department table.

    如果从应用程序代码中删除持久属性,它将在应用程序界面中不可见。但是,相应的列仍将存在于应用程序数据库中。要删除列,请使用模块更新器类公开的 DropColumn 保护方法。此方法删除指定表上的指定列。以下代码段说明了如何从"部门"表中删除"会议室"列。

    public class Updater : ModuleUpdater {
        // ...
        public override void UpdateDatabaseBeforeUpdateSchema() {
            base.UpdateDatabaseBeforeUpdateSchema();
            if (CurrentDBVersion < new Version("1.1.0.0"))
                && CurrentDBVersion > new Version("0.0.0.0")) {
                DropColumn("Department", "Room");
            }
        }
        // ...

    The "1.1.0.0" string is the application version in which changes are introduced. So, the column will be removed only if the database version is less than "1.1.0.0". Checking to determine if the database version is greater then "0.0.0.0" is required to handle a scenario when the database is empty or does not exist.

    "1.1.0.0"字符串是引入更改的应用程序版本。因此,仅当数据库版本小于 "1.1.0.0]"时,才会删除该列。检查以确定数据库版本是否更大,然后需要"0.0.0.0.0"来处理数据库为空或不存在时的方案。

    The SQL command actually executed within this example is:

    此示例中实际执行的 SQL 命令是:

    ALTER TABLE dbo.Department DROP COLUMN [Room]

    The SQL command text is written to the application Log File with the "ExecuteNonQueryCommand:" prefix before being executed. If an error occurs when executing the DropColumn method, the exception text is also logged. However, application execution is not interrupted. You can refer to the application log file for debugging purposes.

    SQL 命令文本使用"执行NonQuery命令:"写入应用程序日志文件。执行前缀。如果执行 DropColumn 方法时出现错误,也会记录异常文本。但是,应用程序执行不会中断。出于调试目的,可以参考应用程序日志文件。

    Note 注意
    Removing a property affects the Layout of Detail Views where the property was previously visible. Layout adjustments may be required after removing a property.
    删除属性会影响以前可见的"详细视图布局"。删除属性后可能需要调整布局。

    You can check that the column was removed during a database update. If your database server is Microsoft SQL Server, run Microsoft SQL Management Studio and navigate to the modified table in the Object Explorer. If you use another database server, use the appropriate database tool to check if the column was actually removed.

    可以检查列在数据库更新期间是否已删除。如果数据库服务器是 Microsoft SQL Server,请运行 Microsoft SQL 管理工作室并导航到对象资源管理器中的已修改表。如果使用其他数据库服务器,请使用相应的数据库工具检查列是否实际删除。

    Change the Persistent Property's Data Type

    更改持久属性的数据类型

    Lets assume you have the Department class with the Description string property.

    假设您具有具有"描述"字符串属性的部门类。

    public class Department : BaseObject {
        // ...
        private string description;
        public string Description {
            get { return description; }
            set { SetPropertyValue(nameof(Description), ref description, value); }
        }
        // ...

    100 characters is the default size of a text property in XPO. For instance, if your database server is Microsoft SQL Server 2005, a column with the nvarchar(100) data type is created for the string property.

    100 个字符是 XPO 中文本属性的默认大小。例如,如果数据库服务器是 Microsoft SQL Server 2005,则为字符串属性创建具有 nvarchar(100) 数据类型的列。

    HowToHandleDbChanges2

    When the property's column is initially created, the data type can be specified by adding the Size or DbType attribute to the property's declaration (refer to the How to Increase the Text Field Size of a Persistent Object knowledge base article). To change the data type of an existing column, use the ExecuteNonQueryCommand protected method exposed by the ModuleUpdater class. This method executes the specified SQL statement. The following snippet illustrates how to set the Description column data type to nvarchar(200).

    最初创建属性列时,可以通过将 Size 或 DbType 属性添加到属性的声明来指定数据类型(请参阅如何增加持久对象知识库文本字段大小一文)。要更改现有列的数据类型,请使用 ModuleUpdater 类公开的 ExecuteNonQuery 命令受保护方法。此方法执行指定的 SQL 语句。以下代码段说明了如何将"描述"列数据类型设置为 nvarchar(200)。

    public class Updater : ModuleUpdater {
        // ...
        public override void UpdateDatabaseBeforeUpdateSchema() {
            base.UpdateDatabaseBeforeUpdateSchema();
            if (CurrentDBVersion < new Version("1.1.0.0") 
                && CurrentDBVersion > new Version("0.0.0.0"))  {
                ExecuteNonQueryCommand(
                    "alter table Department alter column Description nvarchar(200)", true);
            }
        }
        // ...

    The "1.1.0.0" string is the application version in which changes are introduced. So, the data type will be changed only if the database version is less than "1.1.0.0". It is required to check if the database version is greater then "0.0.0.0" to make changes only when the database exists and is filled with data (the empty database version is "0.0.0.0", by default). Available data types depend on the database server, so ensure that the specified data type is valid. The SQL command text is written to the application Log File with the "ExecuteNonQueryCommand:" prefix before being executed. If an error occurs when executing the ExecuteNonQueryCommand method, an exception text is also logged. However, application execution is not interrupted. You can refer to the application log file for debugging purposes. If it is required to throw an exception when an error occurs, set the second parameter of the ExecuteNonQueryCommand method to false.

    "1.1.0.0"字符串是引入更改的应用程序版本。因此,只有当数据库版本小于"1.1.0.0"时,才会更改数据类型。需要检查数据库版本是否大于"0.0.0.0",以便仅在数据库存在且填充数据时进行更改(默认情况下,空数据库版本为 "0.0.0.0]"。)。可用数据类型取决于数据库服务器,因此请确保指定的数据类型有效。SQL 命令文本使用"执行NonQuery命令:"写入应用程序日志文件。执行前缀。如果执行执行NonQueryCommand方法时出现错误,也会记录异常文本。但是,应用程序执行不会中断。出于调试目的,可以参考应用程序日志文件。如果发生错误时需要引发异常,则将 ExecuteNonQueryCommand 方法的第二个参数设置为 false。

    Note 注意
    Ensure that the Property Editor allows you to input text of the required size. In the Model Editor, modify the Size property of the BOModel | Department | Description node, if necessary. This attribute specifies the maximum number of characters that can be typed in the Property Editor.
    确保属性编辑器允许您输入所需大小的文本。在模型编辑器中,修改 BOModel 的大小属性 |部门 |描述节点(如有必要)。此属性指定可在属性编辑器中键入的最大字符数。

    You can check that the data type was modified during the database update. If your database server is Microsoft SQL Server, run Microsoft SQL Management Studio and navigate to the modified column in the Object Explorer.

    可以检查数据类型在数据库更新期间是否被修改。如果数据库服务器是 Microsoft SQL Server,请运行 Microsoft SQL 管理工作室并导航到对象资源管理器中修改的列。

    HowToHandleDbChanges3

    If you use another database server, use the appropriate database tool to check if the data type was actually modified.

    如果使用其他数据库服务器,请使用相应的数据库工具检查数据类型是否实际修改。

    Rename the Persistent Class

    Lets assume you have a Department persistent class that should be renamed to Division. The following steps are required to rename the class and handle the required database and application model changes:

    • rename the class in C# or VB sources,
    • rename the class in XAFML files,
    • rename the database table holding the renamed class data and update the XPObjectType table,

    重命名持久类

    假设您有一个应重命名为"分区"的部门持久性类。需要以下步骤来重命名类并处理所需的数据库和应用程序模型更改:

    • 在 C# 或 VB 源中重命名类,
    • 重命名 XAFML 文件中的类,
    • 重命名包含重命名的类数据的数据库表并更新 XPObjectType 表,

    These steps are detailed below.

    • Rename the Class in C# or VB Sources

    Use the Visual Studio Refactor| Rename... command to look through your solution sources and rename the class anywhere it is used.

     

    这些步骤将在下面详细说明。

    • 在 C# 或 VB 源中重命名类

    使用可视化工作室重构*重 命名。。。命令查看解决方案源,并在使用任何位置重命名类。

    HowToHandleDbChanges6

    Rebuild the solution to ensure that it is compilable, but do not run the application to avoid creating a new table for the new class name.

    重新生成解决方案以确保它是可编译的,但不要运行应用程序以避免为新类名创建新表。

    • Rename the Class in XAFML and BO Files

    • 重命名 XAFML 和 BO 文件中的类

      Refactoring tools do not update XAFML code. Use the Find and Replace dialog to look through XAFML files' code to rename the class anywhere it is used. This dialog is available in Visual Studio using the Edit | Find and Replace | Quick Replace menu command or the CTRL-H shortcut.

    重构工具不会更新 XAFML 代码。使用"查找和替换"对话框查看 XAFML 文件代码,以在使用任何位置重命名类。此对话框在可视化工作室中使用编辑 |查找和替换 |快速替换菜单命令或 CTRL-H 快捷方式。

    HowToHandleDbChanges9

    Note 注意
    The List View and Detail View IDs should be modified. For instance, the "Department_ListView" ID should be renamed to "Division_ListView". So, do not check the "Match whole word" option.
    应修改列表视图和详细信息视图 ED。例如,"Department_ListView"ID 应重命名为"Division_ListView"。因此,不要选中"匹配整个单词"选项。
    • Rename the Database Table Holding the Renamed Class Data and Update the XPObjectType Table

    • 重命名保存重命名的类数据的数据库表并更新 XPObjectType 表

    The Department table in the database should be renamed before updating the database schema.

    在更新数据库架构之前,应重命名数据库中的"部门"表。

    HowToHandleDbChanges8

    The XPObjectType table, automatically created and containing all the valid persistent object types, should also be modified.

    还应修改自动创建并包含所有有效持久对象类型的 XPObjectType 表。

    HowToHandleDbChanges7

    The ModuleUpdater.UpdateDatabaseBeforeUpdateSchema method is intended to update an application database before the database schema is updated. We will override this method to perform the required changes in the database structure. The ModuleUpdater class exposes the RenameTable and UpdateXPObjectType protected methods. The RenameTable method renames the specified table. The first parameter is the old name and the second is the new name. The UpdateXPObjectType method updates the XPObjectType table. This method finds the row where the TypeName column value equals the first parameter. In this row, it changes the TypeName column to the value specified by the second parameter and changes the AssemblyName column to the value specified by the third parameter. The following snippet illustrates how to rename the Department table to Division and update the XPObjectType table.

    模块更新器.Update 数据库之前更新架构方法用于在更新数据库架构之前更新应用程序数据库。我们将重写此方法以在数据库结构中执行所需的更改。模块更新器类公开重命名表和 UpdateXPObjectType 受保护方法。重命名表方法重命名指定的表。第一个参数是旧名称,第二个是新名称。UpdateXPObjectType 方法更新 XPObjectType 表。此方法查找 TypeName 列值等于第一个参数的行。在此行中,它将 TypeName 列更改为第二个参数指定的值,并将程序集名称列更改为第三个参数指定的值。以下代码段说明了如何将部门表重命名为"部门"表并更新 XPObjectType 表。

    public class Updater : ModuleUpdater {
        // ...
        public override void UpdateDatabaseBeforeUpdateSchema() {
            base.UpdateDatabaseBeforeUpdateSchema();
            if (CurrentDBVersion < new Version("1.1.0.0") 
                && CurrentDBVersion > new Version("0.0.0.0")) {
                RenameTable("Department", "Division");
                UpdateXPObjectType(
                    "MySolution.Module.Department", "MySolution.Module.Division", "MySolution.Module");
            }
        }
        // ...

    The "1.1.0.0" string is the application version in which changes are introduced. So, the column will be removed only if the database version is less than "1.1.0.0". Checking if the database version is greater than "0.0.0.0" is required to make changes only when the database exists and is filled with data.

    "1.1.0.0"字符串是引入更改的应用程序版本。因此,仅当数据库版本小于 "1.1.0.0]"时,才会删除该列。仅当数据库存在且数据填充时,才需要检查数据库版本是否大于"0.0.0.0"。。

    The SQL commands actually executed within this example are:

    此示例中实际执行的 SQL 命令是:

    sp_rename 'Department', 'Division', 'OBJECT'
    
    update XPObjectType set TypeName = 'MySolution.Module.Division', AssemblyName = 'MySolution.Module'
    
    where TypeName = 'MySolution.Module'

    The SQL command text is written to the application Log File with the "ExecuteNonQueryCommand:" prefixes before being executed. If an error occurs when executing the RenameTable or UpdateXPObjectType method, the exception text is also logged. However, in the case of an error, the application execution is not interrupted. You can refer to the application log file for debugging purposes.

    SQL 命令文本使用"执行NonQuery命令:"写入应用程序日志文件。执行前缀。如果执行重命名表或 UpdateXPObjectType 方法时出现错误,也会记录异常文本。但是,如果出现错误,应用程序执行不会中断。出于调试目的,可以参考应用程序日志文件。

    Increment the application version, run the application and ensure that the class name changed and data is available.

    增加应用程序版本,运行应用程序并确保类名称已更改且数据可用。

    You can check that the class' table name was modified during the database update. If your database server is Microsoft SQL Server, run Microsoft SQL Management Studio and navigate to the modified table in the Object Explorer.

    您可以在数据库更新期间检查类的表名称是否被修改。如果数据库服务器是 Microsoft SQL Server,请运行 Microsoft SQL 管理工作室并导航到对象资源管理器中的已修改表。

    HowToHandleDbChanges8_1

    If you use another database server, use the appropriate database tool to check if the table name was actually modified.

    如果使用其他数据库服务器,请使用相应的数据库工具检查表名称是否实际修改。

    Rename the Persistent Class Participant in Many-to-Many Relationship

    重命名多对多关系中的持久类参与者

    Lets assume you have the Department and Position classes and there is a Department-Position many-to-many relationship. It is required to rename the Department to Division.

    假设您有"部门和职位"类,并且存在"部门定位多对多"关系。需要将该部改名为司。

    [DefaultClassOptions ]
    public class Department : BaseObject {
        // ...
        [Association("Departments-Positions")]
        public XPCollection<Position> Positions {
            get { return GetCollection<Position>(nameof(Positions)); }
        }
        // ...
    }
    [DefaultClassOptions]
    public class Position : BaseObject {
        // ...
        [Association("Departments-Positions")]
        public XPCollection<Department> Departments {
            get { return GetCollection<Department>(nameof(Departments)); }
        }
        // ...
    }

    All the steps described in the Rename a Persistent Class section are required in this case. But, there are two additional steps.

    • Rename the Position.Departments property to Divisions and modify the property name used in this property's getter. Change the "Departments-Positions" association name to "Divisions-Positions".
    • Modifying the PositionPositions_DepartmentDepartments database table while storing the relationship information is required - rename the Departments column and the table itself.

    在这种情况下,需要重命名持久类部分中描述的所有步骤。但是,还有两个额外的步骤。

    • 将"位置.部门"属性重命名为"部门",并修改此属性的 getter 中使用的属性名称。将"部门-职位"关联名称更改为"部门-职位"。

    • 在存储关系信息时修改PositionPositions_DepartmentDepartments数据库表 - 重命名"部门"列和表本身。

    HowToHandleDbChanges12

    To perform the renaming, add the following code to the UpdateDatabaseBeforeUpdateSchema method.

    要执行重命名,请将以下代码添加到 Update 架构之前的 UpdateDatabase 架构方法。

    if (TableExists("PositionPositions_DepartmentDepartments")) {
        RenameColumn("PositionPositions_DepartmentDepartments", "Departments", "Divisions");
        RenameTable("PositionPositions_DepartmentDepartments", "PositionPositions_DivisionDivisions");
    }

    Rename the Persistent Class Used as the Data Type in Analysis

    重命名用作分析中数据类型的持久类

    If the Analysis business class is used in your application, it may be required to modify the Analysis table in end-user databases. This is necessary because end-users can have Analysis objects with DataType properties pointing to a renamed class. So, after performing the steps described in the Rename a Persistent Class section, add the following code to the UpdateDatabaseBeforeUpdateSchema method.

    如果在应用程序中使用"分析"业务类,则可能需要修改最终用户数据库中的分析表。这是必需的,因为最终用户可以具有指向重命名类的 DataType 属性的分析对象。因此,在执行重命名持久类部分中描述的步骤后,将以下代码添加到 UpdateDatabaseSchema 方法。

    ExecuteNonQueryCommand(
        "update Analysis set ObjectTypeName = 'MySolution.Module.Division' " + 
        "where ObjectTypeName = 'MySolution.Module.Department'", true);

    Rename a Persistent Class Used as the Data Type in Reports

    重命名用作报表中数据类型的持久类

    If the Reports V2 Module is added to your application, it is required to modify the report's data. This is necessary because end-users may have reports with the IReportDataV2.DataType property pointing to a renamed class. So, after performing the steps described in the Rename a Persistent Class section, add the following code to the UpdateDatabaseBeforeUpdateSchema method.

    如果报表 V2 模块添加到应用程序中,则需要修改报表的数据。这是必需的,因为最终用户可能具有指向重命名类的 IReportDataV2.DataType 属性的报告。因此,在执行重命名持久类部分中描述的步骤后,将以下代码添加到 UpdateDatabaseSchema 方法。

    using DevExpress.ExpressApp.ReportsV2;
    //...
    ReportDataProvider.MassUpdateDataType<ReportDataV2>(
        ObjectSpace, "MySolution.Module.Department", typeof(Division));

    In the code above, the ReportDataProvider.MassUpdateDataType<T> method is used instead of ExecuteNonQueryCommand.

    在上面的代码中,使用报表数据提供程序.MassUpdateDataType<T> 方法而不是执行NonQuery命令。

    Remove the Persistent Class

    删除持久类

    If you remove the persistent class from the application code, it will not be visible in the application interface. But, the corresponding table will still exist in the application database. To remove the table, use the DropTable protected method exposed by the ModuleUpdater class. This method removes the specified table. The following snippet illustrates how to remove the Division table.

    如果从应用程序代码中删除持久类,它将在应用程序接口中不可见。但是,相应的表仍将存在于应用程序数据库中。要删除表,请使用模块更新器类公开的 DropTable 保护方法。此方法删除指定的表。以下代码段说明了如何删除分区表。

    public class Updater : ModuleUpdater {
        // ...
        public override void UpdateDatabaseBeforeUpdateSchema() {
            base.UpdateDatabaseBeforeUpdateSchema();
            if (CurrentDBVersion < new Version("1.1.0.0")
                && CurrentDBVersion > new Version("0.0.0.0")) {
                DropTable("Division", true);
                DeleteObjectType("MySolution.Module.Division");
            }
        }
        // ...

    The "1.1.0.0" string is the application version in which changes are introduced. So, the table will be removed only if the database version is less than "1.1.0.0". It is required to check if the database version is greater then "0.0.0.0" to make changes only when the database exists and is filled with data (the empty database version is "0.0.0.0", by default).

    "1.1.0.0"字符串是引入更改的应用程序版本。因此,仅当数据库版本小于 "1.1.0.0]"时,才会删除该表。需要检查数据库版本是否大于"0.0.0.0",以便仅在数据库存在且填充数据时进行更改(默认情况下,空数据库版本为 "0.0.0.0]"。)。

    The SQL commands actually executed within this example are:

    此示例中实际执行的 SQL 命令是:

    drop table Division
    
    delete from XPObjectType where TypeName = 'MySolution.Module.Division'

    The SQL commands text is written to the application Log File with the "ExecuteNonQueryCommand:" prefix, before being executed. If an error occurs when executing the DropTable or DeleteObjectType method, the exception text is also logged. However, application execution is not interrupted. You can refer to the application log file for debugging purposes. If it is required to throw an exception if an error occurs when deleting the table, set the second parameter of the DropTable method to false.

    SQL 命令文本使用"执行NonQuery命令:"写入应用程序日志文件。前缀,然后再执行。如果执行 DropTable 或 DeleteObjectType 方法时出现错误,也会记录异常文本。但是,应用程序执行不会中断。出于调试目的,可以参考应用程序日志文件。如果在删除表时发生错误,则需要引发异常,将 DropTable 方法的第二个参数设置为 false。

    You can check that the table was removed during the database update. If your database server is Microsoft SQL Server, run Microsoft SQL Management Studio and navigate to the application database table list in the Object Explorer. If you use another database server, use the appropriate database tool to check if the table was actually removed.

    可以检查表在数据库更新期间是否已删除。如果数据库服务器是 Microsoft SQL Server,请运行 Microsoft SQL 管理工作室并导航到对象资源管理器中的应用程序数据库表列表。如果使用其他数据库服务器,请使用相应的数据库工具检查表是否实际删除。

  • 相关阅读:
    [leedcode 46] Permutations
    [leedcode 45] Jump Game II
    [leedcode 43] Multiply Strings
    [leedcode 42] Trapping Rain Water
    [leedcode 41] First Missing Positive
    [leedcode 40] Combination Sum II
    全排列
    [leedcode 39] Combination Sum
    [leedcode 38] Count and Say
    调和级数求和(分块)
  • 原文地址:https://www.cnblogs.com/foreachlife/p/How-to-Handle-Renamings-and-Deletions-of-Business-Classes-and-their-Properties.html
Copyright © 2020-2023  润新知