Implement Dependent Reference Properties实现依赖引用属性 (EF)
In this lesson, you will learn how to implement properties whose values can depend on other properties. The Manager property will be added to the Contact class. By default, it will be represented by a lookup editor containing all Contacts that exist in the database. However, you may need this editor to contain Contacts from the same Department. In addition, you may need the Position of these Contacts to be "Manager". To do this, use the DataSourcePropertyAttribute and DataSourceCriteriaAttribute attributes for the Manager property.
在本课中,您将学习如何实现其值可以依赖于其他属性的属性。"管理器"属性将添加到"联系人"类中。默认情况下,它将由包含数据库中存在的所有联系人的查找编辑器表示。但是,您可能需要此编辑器包含来自同一部门的联系人。此外,您可能需要这些联系人的位置是"经理"。为此,请使用 Manager 属性的 DataSourceProperty 属性和数据源标准属性属性。
Note
Before proceeding, take a moment to review the following lessons.
- Inherit from the Business Class Library Class (EF)
- Implement Custom Business Classes and Reference Properties (EF)
- Set a One-to-Many Relationship (EF)
-
Add a new Manager property of the Contact type to the Contact class. Apply the DataSourceProperty attribute to this property, as shown below.
-
注意
在继续之前,请花点时间复习以下课程。 -
从业务类库类 (EF) 继承
-
实现自定义业务类和参考属性 (EF)
-
设置一对多关系 (EF)
-
将"联系人"类型的新的"管理器"属性添加到"联系人"类。将 DataSourceProperty 属性应用于此属性,如下所示。
using DevExpress.Persistent.Base; //... public class Contact : Person { public Contact() { //... Subordinates = new List<Contact>(); } //... [DataSourceProperty("Department.Contacts")] public virtual Contact Manager { get; set; } public virtual IList<Contact> Subordinates { get; set; } }
Note
In EF, you always need to implement both "sides" of the relation. This means that each Contact must have a Manager and a list of Subordinates.
With the DataSourceProperty attribute applied, the Manager lookup editor will contain Contact objects that are specified by the Department object's Contacts property.
- 注意
在 EF 中,始终需要实现关系的两个"边"。这意味着每个联系人都必须有一个经理和一个下属列表。
应用 DataSourceProperty 属性后,Manager 查找编辑器将包含由"部门"对象的"联系人"属性指定的联系人对象。 -
Run the application and select Contact in the drop-down list of the New combo box. The Contact Detail View will be invoked. Specify the Department property and expand the Manager lookup editor. Make sure that the Department property of the listed objects is the same as those you specified above.
-
运行该应用程序,并在"新建组合"框的下拉列表中选择"联系人"。将调用"联系人详细信息"视图。指定"部门"属性并展开"管理器查找"编辑器。确保列出的对象的"部门"属性与您上面指定的对象相同。
-
Apply the DataSourceCriteria attribute to the Contact class' Manager property as shown below.
-
将"数据源标准"属性应用于联系人类的管理器属性,如下所示。
public class Contact : Person { //... [DataSourceProperty("Department.Contacts")] [DataSourceCriteria("Position.Title = 'Manager'")] public virtual Contact Manager { get; set; } }
With the DataSourceCriteria attribute applied, the Manager lookup editor will contain Contact objects that satisfy the criteria specified in the attribute parameter.
-
Run the application. Set the Position property to "Manager" for several Contact objects.
-
应用 DataSourceCriteria 属性后,Manager 查找编辑器将包含满足属性参数中指定的条件的"联系人"对象。
运行应用程序。将"位置"属性设置为多个"联系人"对象的"管理器"。 -
Select Contact in the New (
) button's drop-down list. The Contact Detail View will be invoked. Specify the Department property and expand the Manager lookup editor. Check to make sure that the Position property is set to "Manager" for each of the listed objects.
-
在"新建(new_dropdown_btn)"按钮的下拉列表中选择"联系人"。将调用"联系人详细信息"视图。指定"部门"属性并展开"管理器查找"编辑器。检查以确保每个列出的对象的"位置"属性设置为"管理器"。
-
If the Department property is not specified for a Contact, you can provide another data source for the Manager lookup editor. To do this, specify the second parameter for the DataSourceProperty attribute. In the code below, this parameter is set to the DataSourcePropertyIsNullMode.SelectAll value. You can also set the DataSourcePropertyIsNullMode.SelectNothing or DataSourcePropertyIsNullMode.CustomCriteria values. In the latter case, a third parameter is required to specify a criterion.
-
如果未为联系人指定"部门"属性,则可以为 Manager 查找编辑器提供其他数据源。为此,请为 DataSourceProperty 属性指定第二个参数。在下面的代码中,此参数设置为 DataSourcePropertyIsNullMode。您还可以设置"数据源属性"NullMode。在后一种情况下,需要第三个参数来指定条件。
public class Contact : Person { //... [DataSourceProperty("Department.Contacts", DataSourcePropertyIsNullMode.SelectAll)] [DataSourceCriteria("Position.Title = 'Manager'")] public virtual Contact Manager { get; set; } }
The code above will show all contacts in the Manager lookup editor if the Department property is not specified.
- Run the application and check the results.
- 如果未指定"部门"属性,则上述代码将显示"管理器查找"编辑器中的所有联系人。
运行应用程序并检查结果。
You can see the code demonstrated in this lesson in the MySolution.Module | Data | Contact.cs (Contact.vb) file of the EF Demo (Code First) installed with XAF. By default, the EF Demo (Code First) application is installed in %PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\EFDemoCodeFirst.
您可以在 MySolution.模块中看到本课中演示的代码。数据 |Contact.cs(Contact.vb)文件与XAF一起安装的EF演示(代码优先)文件。默认情况下,EF 演示(代码优先)应用程序安装在 %PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\EFDemoCodeFirst中。
XAF开发成品案例参考
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
作者博客: http://www.cnblogs.com/foreachlife
欢迎加入CIIP框架\XAF技术应用交流群: 336090194 群文件中有更多相关工具及文档资料
转载请注明出处。多谢!
欢迎加我微信: admiralcn 或扫码:

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端