Extend the Data Model(扩展数据模型)
This lesson explains how to extend your entity class with different properties.
本课介绍如何使用不同的属性扩展实体类。
During this lesson, you will do the following:
在本课中,您将执行以下操作:
- Add properties of different types to the entity class.(向实体类添加不同类型的属性。)
- See how the field editor type depends on the property’s value type.(了解字段编辑器类型如何依赖于属性的值类型。)
1.Add the following properties to the Employee class:
将以下属性添加到员工类:
C#
using System.ComponentModel.DataAnnotations.Schema;
namespace MySolution.Module.BusinessObjects;
[DefaultClassOptions]
public class Employee : BaseObject
{
//...
public virtual DateTime? Birthday { get; set; }
//Use this attribute to hide or show the editor of this property in the UI.
[Browsable(false)]
public virtual int TitleOfCourtesy_Int { get; set; }
//Use this attribute to exclude the property from database mapping.
[NotMapped]
public virtual TitleOfCourtesy TitleOfCourtesy { get; set; }
}
public enum TitleOfCourtesy
{
Dr,
Miss,
Mr,
Mrs,
Ms
}
The NotMapped attribute requires the System.ComponentModel.DataAnnotations.Schema using directive.
NotMap属性需要System.ComponentModel.DataAnnotations.Schema use指令。
2.Add a migration and update the database. See the following section for details: Use a DBMS: Setup Migrations.
添加迁移并更新数据库。有关详细信息,请参阅以下部分:使用DBMS:设置迁移。
3.Run the application and invoke the Employee object’s Detail View. The application displays a date picker for the DateTime type of values (see Birthday) and a combo box for the enumeration fields (see Title Of Courtesy):
运行应用程序并调用员工对象的详细信息视图。应用程序显示DateTime类型值的日期选择器(请参阅生日)和枚举字段的组合框(请参阅标题礼貌):
ASP.NET Core Blazor
Windows Forms
4.Extend the Employee class further with the following properties. Note the attributes that decorate these properties. Use hyperlinks to learn more about the attributes if their meanings are not immediately clear.
使用以下属性进一步扩展员工类。注意装饰这些属性的属性。如果属性的含义不清楚,请使用超链接了解有关属性的更多信息。
C#
using DevExpress.Persistent.Validation;
using System.ComponentModel.DataAnnotations;
namespace MySolution.Module.BusinessObjects;
[DefaultClassOptions]
//Use this attribute to specify the caption format for the objects of the entity class.
[ObjectCaptionFormat("{0:FullName}")]
[DefaultProperty(nameof(FullName))]
public class Employee : BaseObject
{
//...
[SearchMemberOptions(SearchMemberMode.Exclude)]
public String FullName
{
get { return ObjectFormatter.Format(FullNameFormat, this, EmptyEntriesMode.RemoveDelimiterWhenEntryIsEmpty); }
}
[EditorBrowsable(EditorBrowsableState.Never)]
public String DisplayName
{
get { return FullName; }
}
public static String FullNameFormat = "{FirstName} {MiddleName} {LastName}";
//Use this attribute to specify the maximum number of characters that users can type in the editor of this property.
[FieldSize(255)]
public virtual String Email { get; set; }
//Use this attribute to define a pattern that the property value must match.
[RuleRegularExpression(@"(((http|https)\://)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*)|([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})", CustomMessageTemplate = @"Invalid ""Web Page Address"".")]
public virtual string WebPageAddress { get; set; }
//Use this attribute to specify the maximum string length allowed for this data field.
[StringLength(4096)]
public virtual string Notes { get; set; }
}
5.Add a migration and update the database. See the following section for details: Use a DBMS: Setup Migrations.
添加迁移并更新数据库。有关详细信息,请参阅以下部分:使用DBMS:设置迁移。
6.Run the application and invoke the Employee object’s Detail View:
运行应用程序并调用员工对象的详细信息视图:
ASP.NET Core Blazor
Windows Forms
As you can see, XAF places the Notes editor outside of the group box that contains smaller editors. This is the framework’s default behavior for user-resizable editors. If you want to change the layout, use the steps described in the following lesson: Display a Nested Property Editor in a Detail View.
如您所见,XAF将Notes编辑器放在包含较小编辑器的分组框之外。这是框架对用户可调整大小的编辑器的默认行为。如果要更改布局,请使用以下课程中描述的步骤:在详细信息视图中显示嵌套属性编辑器。