• 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 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 BusinessObjectsMySolutionDbContext.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 (button_new) or Link (link_btn) button in this tab. The Link button allows you to add references to existing Contact objects.

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

      Tutorial_BMD_Lesson6_1

      To remove a reference to an object from this collection, use the Unlink (unlink_img) 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%DocumentsDevExpress Demos 19.2ComponentseXpressApp FrameworkEFDemoCodeFirst.

    您可以在 MySolution.模块中看到本课中演示的代码。数据 |Contact.cs (Contact.vb) 和 Department.cs (部门.vb) 文件与 XAF 一起安装的 EF 演示(代码优先)。默认情况下,EF 演示(代码优先)应用程序安装在 %PUBLIC%_文档_DevExpress 演示 19.2_组件_eXpressApp 框架_EFDemoCodeFirst 中。

  • 相关阅读:
    买不起笔记本,只好自己动手做一个啦!(转)
    Microsoft.Press.Microsoft.Visual.C.Sharp.2005.Step.by.Step.Oct.2005
    将指定网页添加到收藏夹的方法(c#)
    一个任意获得页面控件的方法
    取客户端MAC地址的方法
    关于在活动目录(ACTIVE DIRECTORY)中创建组织单位和用户
    无 Cookie 的 ASP.NET
    吉祥三宝(设计篇)
    C语言第一次实验报告
    C语言第二次实验作业
  • 原文地址:https://www.cnblogs.com/foreachlife/p/Set-a-One-to-Many-Relationship.html
Copyright © 2020-2023  润新知