In this lesson, you will learn how to set a one-to-many relationship between business objects. The Contact and Department business objects will be related by a one-to-many relationship. You will then learn the basics of automatic user interface construction for referenced objects.

在本课中,您将学习如何在业务对象之间设置一对多关系。联系人和部门业务对象将通过一对多关系关联。然后,您将学习引用对象的自动用户界面构造的基础知识。

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 Many-to-Many Relationship (EF)
  • Add the Department class as shown in the Inherit from the Business Class Library Class (EF) lesson. Replace the auto-generated code with the following.

注意

在继续之前,请花点时间复习以下课程。

  • 从业务类库类 (EF) 继承
  • 实现自定义业务类和参考属性 (EF)
  • 设置多对多关系 (EF)
  • 添加部门类,如"从业务类库类 (EF) 继承"一课中所示。将自动生成的代码替换为以下内容。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using DevExpress.Persistent.Base; namespace MySolution.Module.BusinessObjects {
[DefaultClassOptions]
[DefaultProperty(nameof(Title))]
public class Department {
[Browsable(false)]
public Int32 ID { get; protected set; }
public String Title { get; set; }
public String Office { get; set; }
}
}
  • Register the Department class in DbContext. Edit the BusinessObjects\MySolutionDbContext.cs file as shown below.

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

    public class MySolutionDbContext : DbContext {
    //...
    public DbSet<Department> Departments { get; set; }
    }
  • To implement the "One" part of the Department-Contact relationship, add a virtual Department property to the Contact class.

  • 要实现部门-联系人关系的"一个"部分,请向"联系人"类添加虚拟部门属性。

    public class Contact : Person {
    //...
    public virtual Department Department { get; set; }
    }
  • To implement the "Many" part of the Department-Contact relationship, add the Contacts property to the Department class and initialize it in the constructor.

  • 要实现部门-联系人关系的"许多"部分,请将"联系人"属性添加到"部门"类,并在构造函数中初始化它。

    public class Department {
    public Department() {
    Contacts = new List<Contact>();
    }
    //...
    public virtual IList<Contact> Contacts { get; set; }
    }
  • Run the WinForms or ASP.NET application. Invoke a Detail View for a Department object. You can see the Contacts group. To add objects to the Contacts collection, use the New () or Link () button in this tab. The Link button allows you to add references to existing Contact objects.

  • 运行 WinForms 或ASP.NET应用程序。调用部门对象的详细信息视图。您可以看到"联系人"组。要将对象添加到"联系人"集合,请使用此选项卡中的"新建(button_new)"或"链接(link_btn)"按钮。"链接"按钮允许您添加对现有联系人对象的引用。

    To remove a reference to an object from this collection, use the Unlink () button.

要从此集合中删除对对象的引用,请使用"取消链接(unlink_img)"按钮。

Tip

If you create a new Department and then create a new Contact in the Contacts collection, an associated Department is not immediately visible in the Detail View of the newly created Contact. The link between these objects is added later, when you save the Contact. You can change this behavior using the XafApplication.LinkNewObjectToParentImmediately property. When it is set to true, the link will be created and saved immediately after you click New.

提示
如果创建新的"部门",然后在"联系人"集合中创建新的"联系人",则关联部门不会立即在新创建的"联系人"的"详细信息"视图中显示。保存联系人时,稍后将添加这些对象之间的链接。您可以使用 XafApplication.LinkNewObjectParentParent立即属性更改此行为。设置为 true 时,链接将在单击"新建"后立即创建并保存。

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

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

  1. 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 ...

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

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

  3. 一对多关系domain Model中设置使用AutoMapper时出错

    在使用AutoMapper时,把数据从VO-PO时显示如下错误,错误提示说在一对多关系中已将集合设置为EntityCollection,那么这个是为什么呢. 看下action中的代码,我们可以发现这是 ...

  4. Dynamic CRM一对多关系的数据删除时设置自动删除关联的数据

    在业务实体中主子表非常常见,然后子表可能有会有自己的子表或者多对多关系,在删除的业务场景下,删除主数据,剩余的子数据就成了脏数据, 之前的做法是,监听主表的删除事件,然后在插件中找到其下的子表数据然后 ...

  5. [Fluent NHibernate]一对多关系处理

    目录 写在前面 系列文章 一对多关系 总结 写在前面 上篇文章简单介绍了,Fluent Nhibernate使用代码的方式生成Nhibernate的配置文件,以及如何生成持久化类的映射文件.通过上篇的 ...

  6. EF Code First中的主外键约定和一对一、一对多关系的实现

    对于主外键约定的理解,其实是学习实体间一对一和一对多关系的基础. 1.1 主键(Key)约定 主键的默认约定是:只要字段名为--实体名(类名)+"id"(不区分大小写),这就算是默 ...

  7. 10.Configure One-to-Many(配置一对多关系)【Code-First系列】

    现在,我们将学习怎么配置一对多的关系. Visit Entity Relationship section to understand how EF manages one-to-one, one-t ...

  8. [NHibernate]一对多关系(级联删除,级联添加)

    目录 写在前面 文档与系列文章 一对多关系 一个例子 级联删除 级联保存 总结 写在前面 在前面的文章中,我们只使用了一个Customer类进行举例,而在客户.订单.产品中它们的关系,咱们并没有涉及, ...

  9. [NHibernate]一对多关系(关联查询)

    目录 写在前面 文档与系列文章 一对多查询 总结 写在前面 上篇文章介绍了nhibernate的一对多关系如何配置,以及级联删除,级联添加数据的内容.这篇文章我们将学习nhibernate中的一对多关 ...

随机推荐

  1. 关于腾讯云Centos的一些操作

    安装mysql wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-commun ...

  2. python多线程编程—同步原语入门(锁Lock、信号量(Bounded)Semaphore)

    摘录python核心编程 一般的,多线程代码中,总有一些特定的函数或者代码块不希望(或不应该)被多个线程同时执行(比如两个线程运行的顺序发生变化,就可能造成代码的执行轨迹或者行为不相同,或者产生不一致 ...

  3. Android 插件化开发(一):Java 反射技术介绍

    写在前面:学习插件化开发推荐书籍<Android 插件化开发指南>,本系列博客所整理知识部分内容出自此书. 在之前的项目架构的博文中,我们提到了项目插件化架构,提到插件化架构不得不提的到J ...

  4. java.sql.SQLException: Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.解决方案

    解决方案: 首先查看数据库的版本号,删除旧的jar包,将mysql-connector-java.jar更换成对应版本号 同时在连接数据库的url后加上?useUnicode=true&cha ...

  5. PHP-FPM 使用(含多站点多端口)

    PHP-FPM 使用 PHP-FPM 是在 Linux 环境下用来管理调度 PHP 执行的调度器,源码安装时会自动安装,PHP 安装可参考Centos 下安装 PHP (新) 查看版本 # php-f ...

  6. AES加密原理和AOE工程实践

    在AI业务的开发的过程中,我们常常需要对模型文件进行加密.我们从以下几个方面来说一说AES的加密原理以及AOE里的工程实践. 常见的加密算法 AOE对模型加密需求的思考 AES的加密原理 AOE工程实 ...

  7. Linux创建Jenkins启动脚本以及开机启动服务

    1.jenkins.sh #!/bin/bash ###主要目的用于开机启动服务,不然 启动jenkins.war包没有java -jar的权限 JAVA_HOME=/usr/lib/jdk1.8.0 ...

  8. Python 标准类库-数字和数学模块之decimal使用简介

    标准类库-数字和数学模块之decimal使用简介 by:授客 QQ:1033553122 例子 >>>from decimal import * >>>getcon ...

  9. MS SQL 批量操作

    MS SQL支持 sysobject,因此可以用以下条件语句查询表对象 select Name from sysobjects where xtype='U' and Name like 'dnt_% ...

  10. windows命令行pip报错解决的方法

    今天在新电脑安装python,发现pip无效了,于是乎百度了很多方法,发现原因是pip升级导致的 解决办法,卸载pip重新安装 可以首先执行  python -m ensurepip  然后执行 py ...