The Include method of the Entity Framework dbset<t> and the use of the iqueryable<t> extension method include

Source: Internet
Author: User

When the Entity Framework uses code first, the relationships between entities are already configured, and depending on the actual circumstances, it is necessary to acquire the navigation properties at the same time, such as acquiring the classification attributes (navigation properties), or based on optimization considerations, for example.

Examples include the member entity Class (Member) and the Role entity class (role), where the relationship between role and Member is 1:n, and the console application code is as follows:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using System.data.entity;namespace consoleapplication2{    class program    {        static void Main (string[] args)        {            efcontext<member> efmembercontext = new efcontext<member> ();            var members = efmembercontext.set<member> (). ToList ();            foreach (Member item in)            {                Console.WriteLine (item. role.name);            }            Console.readkey ();}}}    
The Efcontext class, the member class, the role class, and the mapping class are as follows:
Using system;using system.collections.generic;using system.data.entity;using system.linq;using System.Text;using system.threading.tasks;using system.linq.expressions;namespace consoleapplication2{public    partial class Efcontext<t>: DbContext where T:class    {public        efcontext (): Base ("name=myconnectionstring")        {        }        protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)        {            database.setinitializer< Efcontext<t>> (null);            MODELBUILDER.CONFIGURATIONS.ADD (New Membermap ());            MODELBUILDER.CONFIGURATIONS.ADD (New Rolemap ());            Base. Onmodelcreating (ModelBuilder);        }                Public dbset<t> Table {get; set;}        Public iqueryable<t> GetList (expression<func<t,bool>> where)        {            return this. Table.where (Where);}}    }
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; namespace consoleapplication2{public    partial class Member    {public        int Id {get; set;}        public string Name {get; set;}        public string Password {get; set;}        public bool delete{get; set;}        public int Roleid {get; set;}        Public virtual role Role {get; set;}}    }
Using system;using system.collections.generic;using system.data.entity.modelconfiguration;using System.Linq;using system.text;using system.threading.tasks;namespace consoleapplication2{Public    class Membermap: entitytypeconfiguration<member>    {public        membermap ()        } {this            . ToTable ("Member");            This. Haskey (M = m.id);            This. hasrequired (M = m.role). Withmany (r = r.members). Hasforeignkey (M = M.roleid);}}    
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; namespace consoleapplication2{public    partial class Role    {public        int Id {get; set;}        public string Name {get; set;}        Public virtual icollection<member> members {get; set;}}}    
Using system;using system.collections.generic;using system.data.entity.modelconfiguration;using System.Linq;using system.text;using system.threading.tasks;namespace consoleapplication2{Public    class Rolemap: entitytypeconfiguration<role>    {public        rolemap ()        } {this            . ToTable ("Role");            This. Haskey (r = r.id);}}}    

The existing data in the database is as follows:

Run the program and see the following statement generated in SQL Profiler:

1. Execute efmembercontext.dbset<member> (). ToList (); Generated sql:

2. The Foreach loop is five times, generating one SQL at a time, as follows:

This omission describes three of these sql,sql statements similar, just read the role attribute when the information is obtained according to different Roleid, so only the parameter values are different

From the above example can be seen in the loop several times in order to get the navigation properties generated a few SQL, if the database table has a large amount of data, such a way to the performance of the impact can be imagined. According to the above example, there is now a way to get the role information while acquiring member, that is, the generated SQL is a two table inner join, so the include has played a role. The code in the main () method is rewritten as follows:

var members = efmembercontext.set<member> (). Include ("Role"). ToList ();

The above is to get member when the navigation property role information, so that in the build SQL is inner join, run the program again, here you can see, in SQL Profiler only once SQL, and SQL is a inner join form

The console output of the above two methods is as follows:

There may be some doubts here, such as the EF generic class encapsulates a property that does not expose the dbset<t> type, or only the return of the iqueryable<t> type, or dbset<t> (). Where (E = true) What do I think about include?

If you are using the Entity Framework,system.data.entity.queryableextensions class to encapsulate the iiqueryable<t> extension method, which includes an include extension method, The reference namespace System.Data.Entity can be used.

Here is the information about getting Roleid<5 's membership and the corresponding role information:

efcontext<member> efmembercontext = new efcontext<member> (); var members = Efmembercontext.set<member > (). Where (M =>m.roleid < 5). Include ("Role"). ToList (); foreach (Member item in) {    Console.WriteLine ("{0}:{1}", item. Name,item. Role.name);} Console.readkey ();

Run the program as follows:

The Include method of the Entity Framework dbset<t> and the use of the iqueryable<t> extension method include

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.