自学内容网 自学内容网

学习008-02-01-07 Implement Reference Properties(实现引用属性)

Implement Reference Properties(实现引用属性)

This lesson explains the following concepts:
本课解释以下概念:

  • How to implement object references to existing classes.(如何实现对现有类的对象引用。)
  • How XAF generates a UI for referenced classes.(XAF如何为引用的类生成UI。)

Note
Before you proceed, take a moment to review the previous lesson:
在继续之前,请花点时间回顾上一课:

  • Configure a One-to-Many Relationship

Step-by-Step Instructions(分步说明)

1.In the MySolution.Module\Business Objects folder, create the Position 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 Position : BaseObject
    {
        public virtual string Title { get; set; }
    }
}

2.Go to the MySolution.Module\MySolutionDbContext file and add a DbSet of Position type:
转到MySolutions. Module\MySolutionDbContext文件并添加一个位置类型的DbSet:

C#

public class MySolutionEFCoreDbContext : DbContext {
    //...
    public DbSet<Position> Positions { get; set; }
}

3.Add the Position property to the Employee class:
将位置属性添加到员工类:

C#

//...
using System.Collections.ObjectModel;

namespace MySolution.Module.BusinessObjects;

[DefaultClassOptions]
public class Employee : BaseObject {
    //...
    public virtual Position Position { get; set; }
}

The Employee class now exposes the Position reference property. This way, you effectively create a “One-to-Many” relationship between these entity classes.
员工类现在公开了位置引用属性。这样,您可以有效地在这些实体类之间创建“One-to-Many”关系。

4.Add a migration and update the database. See the following section for details: Use a DBMS: Setup Migrations.
添加迁移并更新数据库。有关详细信息,请参阅以下部分:使用DBMS:设置迁移。

5.Run the application.
运行应用程序。

You can see that the navigation control displays the Position item. Click this item to access the Position List View. Click New to open the Position Detail View.
您可以看到导航控件显示位置项。单击此项访问位置列表视图。单击新建打开位置详细信息视图。

6.Create Developer and Manager positions. They now appear in the Position List View:
创建开发人员和经理职位。它们现在出现在职位列表视图中:

ASP.NET Core Blazor
在这里插入图片描述

Windows Forms
在这里插入图片描述

7.Open the Employee Detail View. In this view, XAF creates a lookupeditor for the Position reference property.
打开员工详细信息视图。在此视图中,XAF为位置引用属性创建查找编辑器。

ASP.NET Core Blazor
在这里插入图片描述

Windows Forms
在这里插入图片描述

Exercise: Add an “Address” Reference Property(练习:添加“Address”引用属性)

1.Use the same steps to add an Address reference property to the Employee entity. You can find the type declaration in the code sample below.
使用相同的步骤将地址引用属性添加到员工实体。您可以在下面的代码示例中找到类型声明。

Tip
Remember to register the new entity in DbContext, add a new property to the Employee class, and create a new migration for the database.
请记住在DbContext中注册新实体,向员工类添加新属性,并为数据库创建新的迁移。

C#

using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
using System.ComponentModel;

namespace MySolution.Module.BusinessObjects;

[DefaultProperty(nameof(FullAddress))]
public class Address : BaseObject
{
    private const string defaultFullAddressFormat = "{Country}; {StateProvince}; {City}; {Street}; {ZipPostal}";

    public virtual String Street { get; set; }
    public virtual String City { get; set; }
    public virtual String StateProvince { get; set; }
    public virtual String ZipPostal { get; set; }
    public virtual String Country { get; set; }
    
    public String FullAddress
    {
        get { return ObjectFormatter.Format(defaultFullAddressFormat, this, EmptyEntriesMode.RemoveDelimiterWhenEntryIsEmpty); }
    }
}

This class is not visible in the navigation control, but you can create objects of this type from the reference property lookup editor.
此类在导航控件中不可见,但您可以从引用属性查找编辑器创建此类型的对象。

2.Run the application and open the Employee Detail View. It should now contain the Address field:
运行应用程序并打开员工详细信息视图。它现在应该包含地址字段:

ASP.NET Core Blazor
在这里插入图片描述

Windows Forms
在这里插入图片描述

Next Lesson(下一课)

Initialize Entity Properties
初始化实体属性


原文地址:https://blog.csdn.net/thomastang200912_126/article/details/140553542

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!