Venciity

Untitled

Mar 12th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. namespace EntityFrameworkPerformanceDemos
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Data.Entity;
  7.  
  8.     using Ads.Entities;
  9.  
  10.     public class EntityFrameworkPerformanceDemos
  11.     {
  12.         static void Main()
  13.         {
  14.             // Problem 1.   Show Data from Related Tables
  15.             var context = new AdsEntities();
  16.  
  17.             //Console.WriteLine("List ads (with N+1 query problem):");
  18.             //foreach (var ad in context.Ads)
  19.             //{
  20.             //    Console.WriteLine("Title: " + ad.Title + ", Status: " + ad.AdStatus.Status +
  21.             //                      ", Category: " + (ad.Category == null ? "(no category)" : ad.Category.Name) +
  22.             //                      ", Town: " + (ad.Town == null ? "(no town)" : ad.Town.Name) +
  23.             //                      ", User: " + ad.AspNetUser.Name);
  24.             //}
  25.  
  26.             Console.WriteLine("List ads (without N+1 query problem):");
  27.             foreach (var ad in context.Ads.Include(a => a.AdStatus)
  28.                                           .Include(a => a.Category)
  29.                                           .Include(a => a.Town)
  30.                                           .Include(a => a.AspNetUser))
  31.             {
  32.                 Console.WriteLine("Title: " + ad.Title + ", Status: " + ad.AdStatus.Status +
  33.                                   ", Category: " + (ad.Category == null ? "(no category)" : ad.Category.Name) +
  34.                                   ", Town: " + (ad.Town == null ? "(no town)" : ad.Town.Name) +
  35.                                   ", User: " + ad.AspNetUser.Name);
  36.             }
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment