Problems arising from using Include in EntityFramework

Source: Internet
Author: User
This problem occurs in the processing of hierarchical queries. Based on previous experience, when querying the subset B [] of A and A, join the score to Two Queries quickly, however, when there are many sub-sets and so on, the time is not linear growth but an index, because the efficiency load caused by repeated data increases. For example, the first type of unit test time is about 4-6, when we add three child sets, we will be in the range of 14-22, while the practice in Ef is to set up unicon for each join result. It is strange to look at the large string of data without being slow, but we should be separated.

 

Time

 

Test code:

Public class ModelTesting
{
Private EFContext _ dbContext;
Public ModelTesting ()
{
String cnn = System. Configuration. ConfigurationManager. ConnectionStrings [0]. ConnectionString;

_ DbContext = new EFContext (cnn );

}
[Fact]
Public void Test ()
{
Var key = 1000;
Var uid = new Guid ("3905858E-A32E-DF11-BA8F-001CF0CD104B ");
Var myResume = _ dbContext. Set <MyUser> ()
. Single (u => u. UserId = uid );
MyResume. Works. ToList ();
MyResume. Resumes. ToList ();
MyResume. Projects. ToList ();
MyResume. Jobs. ToList ();
MyResume = _ dbContext. Set <MyUser> ()
. Include ("Resumes ")
. Include ("Jobs ")
. Include ("Projects ")
. Include ("Works ")
. Single (u => u. UserId = uid );

}
}

Public class EFContext: DbContext
{
Public EFContext (string cnn): base (cnn)
{
This. Configuration. LazyLoadingEnabled = true;
}

Protected override void OnModelCreating (DbModelBuilder modelBuilder)
{
Base. OnModelCreating (modelBuilder );
ModelBuilder. Entity <MyResume> ();
ModelBuilder. Entity <MyWorkExper> ();
ModelBuilder. Entity <MyProject> ();
ModelBuilder. Entity <MyUser> ();
}
}
[Table ("Resume", Schema = "dbo")]
Public class MyResume
{
[Key]
Public int Key {get; set ;}
[ForeignKey ("Owner")]
Public Guid UserId {get; set ;}
Public MyUser Owner {get; set ;}
}

[Table ("WorkExperience", Schema = "Resume")]
Public class MyWorkExper
{
[Key]
Public int WorkId {get; set ;}
[ForeignKey ("Owner")]
Public Guid UserId {get; set ;}
Public MyUser Owner {get; set ;}
}
[Table ("project", Schema = "Resume")]
Public class MyProject
{
[Key]
Public int ProjectId {get; set ;}
[ForeignKey ("Owner")]
Public Guid UserId {get; set ;}
Public MyUser Owner {get; set ;}
}
[Table ("EnterpriseUser", Schema = "Offer")]
Public class MyUser
{
[Key]
[Column ("UserGuid")]
Public Guid UserId {get; set ;}
Public virtual ICollection <MyResume> Resumes {get; set ;}
Public virtual ICollection <MyProject> Projects {get; set ;}
Public virtual ICollection <MyWorkExper> Works {get; set ;}
Public virtual ICollection <MyJob> Jobs {get; set ;}
}
[Table ("Offer", Schema = "Offer")]
Public class MyJob
{
[Key]
Public long Key
{
Get;
Set;
}
[ForeignKey ("Owner")]
[Required]
Public Guid UserID {get; set ;}

Public virtual MyUser Owner {get; set ;}
}

 

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.