Set a One-to-Many Relationship-many relationship set (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.

In this lesson, you will learn how to set up many relationship between business objects. Contact business objects and associate department through many relationship. Then, you will learn the basics of the referenced object automatic user interface structure.

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.

note

Before proceeding, please take a moment to review the following courses.

  • (EF) business inherited from class library
  • Traffic class and implement custom reference attributes (EF)
  • Set-many relationship (EF)
  • Add department class, such as "from a business class library (EF) inheritance" as shown in a lesson. The automatically generated code replaced with the following.
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.

  • Registration department in DbContext in class. _MySolutionDbContext.cs edit business object file, as shown in FIG.

    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.

  • To achieve the department - "a" relationship of some of the contacts, please "contact" category to add a virtual department property.

    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.

  • To achieve department - contact relationships of "many" section, change the "Contacts" attribute to the "Department" category, 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.

  • Run WinForms or ASP.NET applications. For more information call the department view objects. You can see the "Contacts" group. To add objects to the "Contacts" collection, please use this tab in the "New (button_new)" or "link (link_btn)" button. "Link" button allows you to add a reference to an existing contact object.

    Tutorial_BMD_Lesson6_1

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

          To delete a reference to the object from this collection, use the "unlink (unlink_img)" button.

 

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.

You can see the code demonstrated in this lesson MySolution. Module. Data | Contact.cs (Contact.vb) and Department.cs (department .vb) file installed with EF XAF demo (code first). By default, EF Demo (Code priority) application installed% PUBLIC% _ presentation document _DevExpress 19.2_ _eXpressApp frame assembly in _EFDemoCodeFirst.

Guess you like

Origin www.cnblogs.com/foreachlife/p/Set-a-One-to-Many-Relationship.html