Set a Many-to-Many Relationship-many relationship set (EF)

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.

In this lesson, you will learn how to set up relationships between business objects. To this end, the Task achieve business class, and set up many relationship between the "Contacts" and "mission" object. You will also learn the basics of the user interface automatically construct an object reference.

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.

Note
Before continuing, please take a moment to review the (EF) program inherited from the business class library class.

  • To add to the Task class business applications that can use the Business Class Library "mission" category. Because you need to set the relationship between the "Contacts" and "mission" object, so you need to customize the Task class implementation. And adding the class inherits the "Contacts" attribute set, as shown in the following code.
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.

Please note, TrackedBy collection of statement Contact object's virtual property, and initialize in the constructor.

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

    Note
    applied DemoTask class model default Properties for the Caption property specifies BOModel application model of "Task" value | demonstration task node. In general, you can specify any property BOModel the application model | <Class> node or BOModel | <Class> * own members | by ModelDefault property applies to business class or its members, <Member> node.

     

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

  • Sign-up demo task type in DbContext in. _MySolutionDbContext.cs edit business object file, as shown in FIG.

    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.

  • Modify Contact mankind to achieve: Add Tasks property to the second part of the relationship. The following code demonstrates the link mankind to achieve the desired changes. Note that the "task" Collection Statement Contact object is a virtual property, and initialize in the constructor.
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.

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

  • Run WinForms or ASP.NET applications. For more information call the contact or view the task details view. Add the task to the contact object "Tracking Task" collection, or add the contact to "task" object "press" collection. To apply the allocation, use the link button that comes with these collections.

 

  • Tracked_Tasks_1
  • Tracked_Tasks_2

 

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.

You can see the code demonstrated in this lesson MySolution. Module. Data | XAF installed together with EF demo (code first) of Contact.cs (Contact.vb) and DemoTask.cs (DemoTask.vb) files. 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-Many-to-Many-Relationship.html