In a Model First data model, object properties are declared in the designer-generated files, and you cannot decorate them with the required built-in attributes directly. The workaround is to apply the MetadataType attribute to a class, create the metadata class, and apply the required attributes to the properties of the metadata. The code below illustrates this approach using the data model from the How to: Use the Entity Framework Model First in XAF example.
在 Model First 数据模型中,对象属性在设计器生成的文件中声明,并且不能直接使用所需的内置属性来修饰它们。解决方法是将 MetadataType 属性应用于类,创建元数据类,并将所需的属性应用于元数据的属性。下面的代码使用 XAF 示例中":首先使用实体框架模型"中的数据模型演示了此方法。
Tip 提示
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E4374
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E4374
.
using System.ComponentModel; using System.ComponentModel.DataAnnotations; using DevExpress.ExpressApp.DC; // ... [MetadataType(typeof(EmployeeMetadata))] public partial class Employee { } public class EmployeeMetadata { [Browsable(false)] public Int32 Id { get; set; } } //... [MetadataType(typeof(TaskMetadata))] public partial class Task { } public class TaskMetadata { [Browsable(false)] public Int32 Id { get; set; } [FieldSize(FieldSizeAttribute.Unlimited)] public String Description { get; set; } }
A reference to System.ComponentModel.DataAnnotations is required to compile the code above. Here, we use the Browsable attribute to hide the Id properties (which are key properties in our data model) from the UI. The FieldSizeAttribute specifies that an unlimited number of characters can be stored in the Task.Description property, so a multiline editor will be used for this property in the UI.
要编译上面的代码,需要引用 System.组件模型.Data 注释。在这里,我们使用可浏览属性从 UI 中隐藏 Id 属性(这是数据模型中的关键属性)。
FieldSize属性指定可以在 Task.描述属性中存储无限数量的字符,因此在 UI 中,将使用多行编辑器来执行此属性。