ABP database--Multiple tables and correlated queries in ABP&EF

Source: Internet
Author: User

This article describes multiple table queries in the ABP.

1. Create an entity

Multi-table queries, which are simple in ABP or EF, here we create a demo, a student entity, a school entity.

There can be many students in the school, and students have a school.

The entity is as follows:

School

 Public classschool:entity<Long>     {                 Public stringName {Get;Set; }  Public stringAddress {Get;Set; }        /// <summary>        ///the students inside the school/// </summary>         PublicList<student> Students {Get;Set; } }

Students

    Publicclass student:entity<long>    {         public  Stringgetset;}
        /// <summary>        /// School        where students are located /// </summary>         Public Get Set ; }    }

2. Create data

Now let's create the data for student and school.

The school data is as follows:

The student data is as follows:

As can be seen, the student name is ALUN1, alun2, alun3 corresponding School1, 2, 1.

3. Querying entities

Below, we use repository to query the results below the application layer appservice.

              //use the Getallincluding method to query all student's information, the result contains the school entity                varListStudent1 = _studentrepository.getallincluding (s=>s.school).                ToList (); //use the GetAll method to query all student's information, the result contains the school entity                varListStudent2 =_studentrepository.getall ().                ToList (); //use the GetAll method to query all school information, and the results include the list students entity                varListSchool1 =_schoolrepository.getall ().                ToList (); //use the Getallincluding method to query all school information, and the results include the list students entity                varListSchool2 = _schoolrepository.getallincluding (s=>s.students). ToList ();

As you can see, the results contain student entities that contain school, and the school containing collection is students.

It is worth noting that the difference between getallincluding and getall here is that getallincluding is a clear indication that the entity I want to query is the entity that contains the other tables that are also queried.

For example, getallincluding (s=>s.school), tell EF, when querying student, please also query its associated School.

If there are multiple entity associations, separate them with a comma ",". For example getallincluding (s=>s.school, S=>s.class)

For ABP, if it is multi-tenant, users and other multi-tenant information can not be queried in the case, please add the following sentence before the query

Currentunitofwork.disablefilter (Abpdatafilters.mayhavetenant, abpdatafilters.musthavetenant);

Because the query for long tenants is that the ABP adds some filter by default, only queries the current logged-on user's information, and the other user's information is filtered out.

So the above meaning is to remove the filter when the multi-tenant query

ABP database--Multiple tables and correlated queries in ABP&EF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.