In this lesson, you will learn how to set relationships between business objects. For this purpose, the Task business class will be implemented and a Many-to-Many relationship will be set between the Contact and Task objects. You will also learn the basics of automatic user interface construction for the referenced objects.

在本课中,您将学习如何设置业务对象之间的关系。为此,将实现 Task 业务类,并在"联系人"和"任务"对象之间设置多对多关系。您还将学习引用对象的自动用户界面构造基础知识。

Note

Before proceeding, take a moment to review the Inherit from the Business Class Library Class (EF) lesson.

  • To add the Task business class to the application, you can use the Task class from the Business Class Library. Since you need to set a relationship between the Contact and Task objects, you need to customize the Task class implementation. Inherit from this class and add the Contacts collection property, as shown in the following code.

注意
在继续之前,请花点时间复习从商务舱库类 (EF) 课程继承。

  • 要将 Task 业务类添加到应用程序,可以使用 Business 类库中的"任务"类。由于您需要设置"联系人"和"任务"对象之间的关系,因此需要自定义 Task 类实现。从此类继承并添加"联系人"集合属性,如以下代码所示。
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF; namespace MySolution.Module.BusinessObjects {
[DefaultClassOptions]
[ModelDefault("Caption", "Task")]
public class DemoTask : Task {
public DemoTask() : base() {
TrackedBy = new List<Contact>();
}
public virtual IList<Contact> TrackedBy { get; set; }
}
}

Note that the TrackedBy collection of Contact objects is declared as a virtual property and is initialized in the constructor.

请注意,Contact 对象的 TrackedBy 集合声明为虚拟属性,并在构造函数中初始化。

  • Note

    The ModelDefaultAttribute attribute, which is applied to the DemoTask class, specifies the "Task" value for the Caption property of the Application Model's BOModel | DemoTask node. Generally, you can specify any property of the Application Model's BOModel | <Class> node or BOModel | <Class> | OwnMembers | <Member> node by applying the ModelDefault attribute to a business class or its member.

    注意
    应用于 DemoTask 类的模型默认属性属性为应用程序模型的 BOModel 的 Caption 属性指定"Task"值 |演示任务节点。通常,您可以指定应用程序模型的 BOModel 的任何属性 |<Class>节点或 BOModel |<Class>*自己的会员 |通过将 ModelDefault 属性应用于业务类或其成员,<Member>节点。

  • Register the DemoTask type in the DbContext. Edit the BusinessObjects\MySolutionDbContext.cs file as shown below.

  • 在 DbContext 中注册演示任务类型。编辑业务对象_MySolutionDbContext.cs 文件,如下所示。

    public class MySolutionDbContext : DbContext {
    //...
    public DbSet<DemoTask> TrackedTasks { get; set; }
    }
  • Modify the Contact class implementation: add the Tasks property as the second part of the relationship. The following code demonstrates the required changes in the Contact class implementation. Note that the Tasks collection of Contact objects is declared as a virtual property and is initialized in the constructor.

  • 修改联系人类实现:将 Tasks 属性添加为关系的第二部分。以下代码演示了联系人类实现中所需的更改。请注意,Contact 对象的"任务"集合声明为虚拟属性,并在构造函数中初始化。
using System.Collections.Generic;
//...
public class Contact : Person {
public Contact() {
Tasks = new List<DemoTask>();
}
//...
public virtual IList<DemoTask> Tasks { get; set; }
}

The code above will automatically generate the required intermediate tables and relationships.

  • Run the WinForms or ASP.NET application. Invoke the Contact Detail View or Task Detail View. Add tasks to a Contact object's Tracked Tasks collection, or contacts to a Task object's Tracked By collection. To apply the assignment, use the Link button that accompanies these collections.

上述代码将自动生成所需的中间表和关系。

  • 运行 WinForms 或ASP.NET应用程序。调用联系人详细信息视图或任务详细信息视图。将任务添加到联系人对象的"跟踪任务"集合,或将联系人添加到"任务"对象的"按"集合中。要应用分配,请使用这些集合附带的链接按钮。

You can see the code demonstrated in this lesson in the MySolution.Module | Data | Contact.cs (Contact.vb) and DemoTask.cs (DemoTask.vb) files 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.模块中看到本课中演示的代码。数据 |与 XAF 一起安装的 EF 演示(代码优先)的Contact.cs (Contact.vb) 和DemoTask.cs (DemoTask.vb) 文件。默认情况下,EF 演示(代码优先)应用程序安装在 %PUBLIC%_文档_DevExpress 演示 19.2_组件_eXpressApp 框架_EFDemoCodeFirst 中。

Set a Many-to-Many Relationship设置多对多关系 (EF)的更多相关文章

  1. Set a One-to-Many Relationship设置一对多关系 (EF)

    In this lesson, you will learn how to set a one-to-many relationship between business objects. The C ...

  2. Set a One-to-Many Relationship设置一对多关系 (XPO)

    In this lesson, you will learn how to set a one-to-many relationship between business objects. The C ...

  3. 13.翻译系列:Code-First方式配置多对多关系【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code- ...

  4. XAF中多对多关系 (XPO)

    In this lesson, you will learn how to set relationships between business objects. For this purpose, ...

  5. Django中多对多关系的orm表设计

    作者的管理 1.设计表结构 出版社 书籍 作者 一个出版社出版多个书籍  1对多 书籍和作者的关系:一个作者写多本书,一本书可以是多个作者写.多对多 1)创建一张表,表中多对多的数据关系.使用 多对多 ...

  6. Django-ORM之ManyToManyField的使用-多对多关系

    表结构设计 多对多关系表创建外键,典型例子:书--作者--出版社,书与作者的关系就可以看作是多对多关系. # 表结构设计 class Book(models.Model): title = model ...

  7. EF Core中如何设置数据库表自己与自己的多对多关系

    本文的代码基于.NET Core 3.0和EF Core 3.0 有时候在数据库设计中,一个表自己会和自己是多对多关系. 在SQL Server数据库中,现在我们有Person表,代表一个人,建表语句 ...

  8. [ SQLAlchemy ] 自我引用型的多对多关系(Self-Referential Many-to-Many Relationship)理解

    参考: https://www.jianshu.com/p/2c6c76f94b88 https://madmalls.com/blog/post/followers-and-followeds/ 实 ...

  9. EF里一对一、一对多、多对多关系的配置和级联删除

    本章节开始了解EF的各种关系.如果你对EF里实体间的各种关系还不是很熟悉,可以看看我的思路,能帮你更快的理解. I.实体间一对一的关系 添加一个PersonPhoto类,表示用户照片类 /// < ...

随机推荐

  1. HttpRunner学习6--使用parameters参数化

    前言 在使用HttpRunner测试过程中,我们可能会遇到这种场景: 账号登录功能,需要输入用户名和密码,设计测试用例后有 N 种组合情况 如果测试组合比较少,比如只有2个,那我们直接在YAML脚本中 ...

  2. 线程提供了一个方法:void join() ,join可以协调线程之间的同步运行。

    package seday09; /** * @author xingsir * 线程提供了一个方法:void join() ,join可以协调线程之间的同步运行. * 此方法允许执行这个方法的线程在 ...

  3. 守护线程,需要通过调用线程方法:setDaemon(boolean on)来进行设置

    package seday08.thread;/*** @author xingsir * 守护线程又称为后台线程,默认创建出来的线程都是普通线程, 守护线程需要通过调用线程方法:setDaemon( ...

  4. oracle数据库解决system表空间已爆满的问题

    有时会发现数据库system表空间增长很快,使用以下语句查看system表空间使用量.也可以使用toad直接看. select b.tablespace_name "表空间", b ...

  5. python基础之列表讲解

    列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现. 列表的数据项不需要具有相同的类型 如下图所示,创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可.(接下来的演 ...

  6. IT兄弟连 HTML5教程 响应式布局实例

    在学习Media Queries模块前,先通过一个响应式布局实例来了解一下响应式布局和Media Queries模块的简单应用.在本例中,使用HTML5的结构元素定义了5个盒子.当浏览器窗口尺寸不同时 ...

  7. Spring Boot 项目维护全局json数据

    1:概述 过去 我们在每一个方法中处理前端发过来的请求,需要自己构造请求数据,然后通过spring 提供的@ResponseBody 强制转为JSON数据吗,实际上出现了很多重复的代码,我么亦可以通过 ...

  8. SQLserver还原失败(数据库正在使用,无法获得对数据库的独占访问权)

    问题描述: Sql server还原失败(数据库正在使用,无法获得对数据库的独占访问权) 数据库还原的时候还有其他进程连在上面,导致无法获得独占造成的. 这个问题的原因在于有用户连接了当前要做还原的数 ...

  9. sap-abap 权限控制

    FORM AUTH_CHECK . "工厂 LOOP AT S_WERKS. AUTHORITY-CHECK OBJECT 'M_BANF_WRK' ID 'WERKS' FIELD S_W ...

  10. default(T);

    在泛型类型中,由于泛型类型即可以是引用类型也可以是值类型,所以不能用null来表示默认值.这里通过default来进行.引用类型的default将泛型类型初始化null,值类型的default将泛型类 ...