学习011-03-09 Handle Renamings and Deletions of Business Classes and their Properties处理业务类及其属性的重命名和删除

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 Microsoft 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.
ModuleUpdater类的一些受保护的方法,如本主题中演示的RenameTable和RenameColull,在您直接连接到MicrosoftSQLServer时适用,并且可能不适用于不同的数据库引擎。如果在使用这些方法时遇到异常,您可以将适当的查询传递给ExecuteNonQueryCommand、ExecuteScalarCommand或ExecuteReader命令。

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属性的部门类,并且需要将此属性重命名为Room。

C#
[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# sources
    重命名C#源代码中的属性
  • rename the property in XAFML and BO files
    重命名XAFML和BO文件中的属性
  • rename the table column holding the renamed property values
    重命名包含重命名属性值的表列

These steps are detailed below.
这些步骤详述如下。

  • Rename the Property in C# sources(重命名C#源中的属性)

Use the Visual Studio Refactor | Rename… command to look through your solution sources and rename the property anywhere it is used.
使用Visual Studio Refactor | Rename…命令查看您的解决方案源并在使用它的任何地方重命名属性。
在这里插入图片描述

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文件以重命名属性。在Visual Studio中,可以使用 Edit | Find和 Replace | Quick Replace菜单命令或CTRL-H快捷方式使用此对话框。例如,如果您有自定义的部门详细信息视图布局,或者如果在筛选条件中使用了Office属性,则可能需要此对话框。否则,自定义项将丢失。
在这里插入图片描述

  • 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列。
在这里插入图片描述

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.
ModuleUpdater.UpdateDatabaseBeforeUpdateSchema方法旨在在更新数据库模式之前更新应用程序数据库。我们将重写此方法以对数据库结构执行所需的更改。ModuleUpdater类公开了RenameColull受保护的方法,该方法重命名数据库表中所需的列。以下代码片段说明了如何将部门表中的Office列重命名为Room。

C#
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命令文本在执行前以“ExecuteNonQueryCommand:”前缀写入应用程序日志文件。如果在执行RenameColull方法时发生错误,也会记录异常文本。但是,在发生错误的情况下,应用程序执行不会中断。您可以参考应用程序日志文件进行调试。

  • 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.
您可以检查在数据库更新期间是否修改了列名。如果您的数据库服务器是MicrosoftSQLServer,请运行MicrosoftSQLManagement Studio并使用对象资源管理器导航到修改后的列。
在这里插入图片描述

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.
如果从应用程序代码中删除持久属性,它将在应用程序界面中不可见。但是,相应的列仍将存在于应用程序数据库中。要删除列,请使用由ModuleUpdater类公开的DropColull protected方法。此方法删除指定表上的指定列。以下代码片段说明了如何从部门表中删除Room列。

C#
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”来处理场景。

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命令文本在执行前被写入前缀为"ExecuteNonQueryCommand:"的应用程序日志文件。如果在执行DropCollie方法时发生错误,也会记录异常文本。但是,应用程序执行不会中断。您可以参考应用程序日志文件以进行调试。

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.
您可以检查该列是否在数据库更新期间被删除。如果您的数据库服务器是MicrosoftSQLServer,请运行MicrosoftSQLManagement Studio并导航到对象资源管理器中的修改表。如果您使用其他数据库服务器,请使用适当的数据库工具检查该列是否确实被删除。

Change the Persistent Property’s Data Type(更改持久性属性的数据类型)

Lets assume you have the Department class with the Description string property.
让我们假设您有带有Description字符串属性的部门类。

C#
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中文本属性的默认大小。例如,如果您的数据库服务器是MicrosoftSQLServer 2005,则会为字符串属性创建一个数据类型为nvarchar(100)的列。

在这里插入图片描述

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类公开的ExecuteNonQueryCommand保护方法。该方法执行指定的SQL语句。以下代码片段说明了如何将Description列数据类型设置为nvarchar(200)。

C#
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命令文本在执行前以“ExecuteNonQueryCommand:”前缀写入应用程序日志文件。如果执行ExecuteNonQueryCommand方法时发生错误,也会记录异常文本。但是,应用程序执行不会中断。您可以参考应用程序日志文件以进行调试。如果发生错误时需要抛出异常,请将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 | Department | Description节点的大小属性。此属性指定可以在属性编辑器中键入的最大字符数。

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.
您可以检查数据类型是否在数据库更新期间被修改。如果您的数据库服务器是MicrosoftSQLServer,请运行MicrosoftSQLManagement Studio并导航到对象资源管理器中的修改列。

在这里插入图片描述

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# sources,
    重命名C#源代码中的类,

  • rename the class in XAFML files,
    重命名XAFML文件中的类,

  • rename the database table holding the renamed class data and update the XPObjectType table,
    重命名保存重命名类数据的数据库表并更新XPObjectType表,

These steps are detailed below.
这些步骤详述如下。

  • Rename the Class in C# Sources(在C#源代码中重命名类)

Use the Visual Studio Refactor| Rename… command to look through your solution sources and rename the class anywhere it is used.
使用Visual Studio Refactor| Rename… 命令查看您的解决方案源并在使用它的任何地方重命名类。
在这里插入图片描述

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文件的代码,以在使用类的任何地方重命名类。此对话框可在Visual Studio中使用编辑|查找和替换|快速替换菜单命令或CTRL-H快捷方式。
在这里插入图片描述

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.
列表视图和详细视图ID应该被修改。例如,“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.
在更新数据库模式之前,应重命名数据库中的部门表。
在这里插入图片描述

The XPObjectType table, automatically created and containing all the valid persistent object types, should also be modified.
还应修改自动创建并包含所有有效持久对象类型的XPObjectType表。
在这里插入图片描述

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.
ModuleUpdater.UpdateDatabaseBeforeUpdateSchema方法旨在在更新数据库模式之前更新应用程序数据库。我们将覆盖此方法以在数据库结构中执行所需的更改。ModuleUpdater类公开了RenameTable和UpdateXPObjectType受保护的方法。RenameTable方法重命名指定的表。第一个参数是旧名称,第二个是新名称。UpdateXPObjectType方法更新XPObjectType表。此方法查找TypeName列值等于第一个参数的行。在这一行中,它将TypeName列更改为第二个参数指定的值,并将AssemblyName列更改为第三个参数指定的值。以下代码片段说明了如何将部门表重命名为部门并更新XPObjectType表。

C#
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命令文本在执行前以"ExecuteNonQueryCommand:"前缀写入应用程序日志文件。如果在执行RenameTable或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.
您可以检查在数据库更新期间类的表名是否被修改。如果您的数据库服务器是MicrosoftSQLServer,请运行MicrosoftSQLManagement Studio并在对象资源管理器中导航到修改后的表。
在这里插入图片描述

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.
假设您有部门和职位类,并且存在部门-职位多对多关系。需要将部门重命名为部门。

C#
[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”.
    需要修改PositionPositions_DepartmentDepartments数据库表,同时存储关系信息-重命名Departments列和表本身。

  • Modifying the PositionPositions_DepartmentDepartments database table while storing the relationship information is required - rename the Departments column and the table itself.
    将Position. Departments属性重命名为Divisions,并修改此属性的getter中使用的属性名称。将“Departments-Positions”关联名称更改为“Divisions-位置”。
    在这里插入图片描述

To perform the renaming, add the following code to the UpdateDatabaseBeforeUpdateSchema method.
要执行重命名,请将以下代码添加到UpdateDatabaseBeforeUpdateSchema方法。

C#
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属性的分析对象。因此,在执行重命名持久类部分中描述的步骤后,将以下代码添加到UpdateDatabaseBeforeUpdateSchema方法中。

C#
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属性的报告。因此,在执行重命名持久类部分中描述的步骤后,将以下代码添加到UpdateDatabaseBeforeUpdateSchema方法中。

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

In the code above, the ReportDataProvider.MassUpdateDataType method is used instead of ExecuteNonQueryCommand.
在上面的代码中,使用ReportDataProvider. MassUpdateDataType方法代替ExecuteNonQueryCommand。

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.
如果从应用程序代码中删除持久类,它将在应用程序界面中不可见。但是,相应的表将仍然存在于应用程序数据库中。要删除该表,请使用ModuleUpdater类公开的DropTable保护方法。该方法删除指定的表。下面的代码片段说明了如何删除分区表。

C#
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命令文本被写入前缀为“ExecuteNonQueryCommand:”的应用程序日志文件,然后再执行。如果在执行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.
您可以检查表是否在数据库更新期间被删除。如果您的数据库服务器是MicrosoftSQLServer,请运行MicrosoftSQLManagement Studio并导航到对象资源管理器中的应用程序数据库表列表。如果您使用其他数据库服务器,请使用适当的数据库工具检查表是否确实被删除。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值