Configure a One-to-Many Relationship(配置一对多关系)
This lesson explains how to create a One-to-Many relationship between two entities and how XAF generates the UI for such a relationship.
本课介绍如何在两个实体之间创建一对多关系以及XAF如何为这种关系生成UI。
Note
Before you proceed, take a moment to review the previous lessons:(在继续之前,请花点时间回顾一下之前的课程:)
Employee-Department Relationship(员工与部门的关系)
1.In the MySolution.Module\Business Objects folder, create the Department class. Replace the generated class declaration with the following code:
在MySolutions. Module\Business Objects文件夹中,创建部门类。将生成的类声明替换为以下代码:
C#
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
using System.ComponentModel;
namespace MySolution.Module.BusinessObjects
{
[DefaultClassOptions]
[DefaultProperty(nameof(Title))]
public class Department : BaseObject
{
public virtual string Title { get; set; }
public virtual string Office { get; set; }
}
}
The code applies the DefaultProperty attribute to the Department class. Use this attribute to specify the most descriptive property of your class. Default property values are displayed in the following UI elements:
该代码将DefaultProperty属性应用于部门类。使用此属性指定类的最具描述性的属性。默认属性值显示在以下UI元素中:
- Detail form captions(详细表格标题)
- The leftmost column of a List View(列表视图最左边的列)
- Lookup List Views(查找列表视图)
Refer to the following topic for more information: Data Annotations in Data Model.
有关详细信息,请参阅以下主题:数据模型中的数据注释。
2.Go to the MySolution.Module\MySolutionDbContext file and add a DbSet of the Department class:
转到MySolutions. Module\MySolutionDbContext文件并添加部门类的DbSet:
C#
public class MySolutionEFCoreDbContext : DbContext {
//...
public DbSet<Department> Departments { get; set; }
}
3.Add the Department reference property to the Employee class:
将部门引用属性添加到员工类:
C#
//...
public class Employee : BaseObject {
//...
public virtual Department Department { get; set; }
}
When you add a reference property of one entity type to another entity type, you establish the “One” part of the relationship between these entities. In this case it is a relationship between the Department and Employee entity classes.
当您将一个实体类型的引用属性添加到另一个实体类型时,您将建立这些实体之间关系的“One”部分。在这种情况下,它是部门和员工实体类之间的关系。
Note that the property has the virtual access modifier for lazy loading implementation. For additional information, refer to the Microsoft documentation: Lazy Loading.
请注意,该属性具有用于延迟加载实现的虚拟访问修饰符。有关其他信息,请参阅Microsoft留档:延迟加载。
4.Add the Employees collection property to the Department class and initialize it in the constructor:
将雇员集合属性添加到部门类并在构造函数中初始化它:
C#
// ...
using System.Collections.ObjectModel;
//...
public class Department : BaseObject {
//..
public virtual IList<Employee> Employees { get; set; } = new ObservableCollection<Employee>();
}
This way, you implement the “Many” part of the relationship between the Department object and the Employee object.
这样,您就实现了部门对象和员工对象之间关系的“Many”部分。
5.Add a migration and update the database. See the following section for details: Use a DBMS: Setup Migrations.
添加迁移并更新数据库。有关详细信息,请参阅以下部分:使用DBMS:设置迁移。
6.Run the application.
运行应用程序。
Open the Department Detail View. You can see the Employees group. This is how XAF renders the Employees collection property.
打开部门详细信息视图。您可以看到员工组。这就是XAF呈现员工集合属性的方式。
To add objects to the Employees collection, use the New or Link button in this tab. The Link button allows users to add references to existing Employee objects.
要将对象添加到员工集合,请使用此选项卡中的新建或链接按钮。链接按钮允许用户添加对现有员工对象的引用。
ASP.NET Core Blazor
Windows Forms
To remove a reference to an object from this collection, select this object and click Unlink.
要从此集合中删除对对象的引用,请选择此对象并单击取消链接。
Tip
If you create a new Department and then create a new Employee in the Employees collection, the associated Department is not immediately visible in the Detail View of the newly created Employee object. The link between these objects is added later when you save the Employee object. To change this behavior, use the XafApplication.LinkNewObjectToParentImmediately property. When the property value is true, the application creates a link and saves it immediately after you click New.
如果创建新部门,然后在员工集合中创建新员工,则关联的部门不会立即在新创建的员工对象的详细信息视图中可见。这些对象之间的链接稍后会在保存员工对象时添加。要更改此行为,请使用XafApplication.LinkNewObjectToParentImmediately属性。当属性值为true时,应用程序会创建一个链接,并在单击新建后立即保存。
7.Open the Employee Detail View. In this view, XAF creates a lookup editor for the Department reference property. Lookup editors support incremental filtering. This editor uses a special type of View — Lookup List View. The Lookup List View includes a single column that displays the values of the default property. In your application, these are the values of the Title property.
打开员工详细信息视图。在此视图中,XAF为部门引用属性创建一个查找编辑器。查找编辑器支持增量过滤。此编辑器使用一种特殊类型的视图-查找列表视图。查找列表视图包括一个显示默认属性值的列。在您的应用程序中,这些是标题属性的值。
ASP.NET Core Blazor
Windows Forms
Note(注)
The most common pattern for a relationship is to define properties on both ends of the relationship. At the same time, according to the conventions of Entity Framework Core, it is sufficient to add only the reference property (the “One” part). It establishes the “One-To-Many” relationship between entities and Entity Framework Core automatically creates a foreign key to the related table in the database.
关系最常见的模式是在关系的两端定义属性。同时,根据实体框架核心的约定,只添加引用属性(“One”部分)就足够了。它建立了实体之间的“One-To-Many”关系,实体框架核心自动创建数据库中相关表的外键。
The main difference between these techniques is how XAF renders the application’s UI. When you omit the “Many” part of the relationship, XAF doesn’t create an editor for the omitted collection property in the Detail View of the entity class. You can see an example of this in the following lesson: Implement Reference Properties.
这些技术之间的主要区别在于XAF如何呈现应用程序的用户界面。当您省略关系的“Many”部分时,XAF不会在实体类的详细信息视图中为省略的集合属性创建编辑器。您可以在以下课程中看到一个示例:实现引用属性。
Exercise: Create an Employee-PhoneNumber Relationship(练习:建立Employee-PhoneNumber关系)
1.Create a PhoneNumber class and implement a One-To-Many relationship between this class and the Employee class. This time the Employee should be the “One” part of the relationship, while the PhoneNumber should be the “Many” part. You can find the type declaration in the code sample below.
创建一个PhoneNumber类,并在这个类和员工类之间实现一对多关系。这次员工应该是关系的“One”部分,而电话号码应该是“Many”部分。您可以在下面的代码示例中找到类型声明。
Tip
Remember to register the new entity in DbContext and create a new migration for the database.
请记住在DbContext中注册新实体并为数据库创建新的迁移。
Use the code sample below to replace the autogenerated class declaration in the PhoneNumber class:
使用下面的代码示例替换PhoneNumber类中的自动生成类声明:
C#
using DevExpress.Persistent.BaseImpl.EF;
using System.ComponentModel;
namespace MySolution.Module.BusinessObjects;
[DefaultProperty(nameof(Number))]
public class PhoneNumber : BaseObject
{
public virtual String Number { get; set; }
public virtual String PhoneType { get; set; }
public override String ToString()
{
return Number;
}
}
This class is not visible in the navigation control.
此类在导航控件中不可见。
2.Run the application. Open the Employee Detail View to see the Phone Numbers group:
运行应用程序。打开员工详细信息视图以查看电话号码组:
ASP.NET Core Blazor
Windows Forms