Skip to content

Commit

Permalink
Use file-scoped namespaces everywhere in samples
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Jan 31, 2022
1 parent 1b1b22b commit a2b61f8
Show file tree
Hide file tree
Showing 360 changed files with 21,906 additions and 22,267 deletions.
119 changes: 59 additions & 60 deletions samples/core/Benchmarks/QueryTrackingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,77 @@
using BenchmarkDotNet.Attributes;
using Microsoft.EntityFrameworkCore;

namespace Benchmarks
{
[MemoryDiagnoser]
public class QueryTrackingBehavior
{
[Params(10)]
public int NumBlogs { get; set; }
namespace Benchmarks;

[Params(20)]
public int NumPostsPerBlog { get; set; }
[MemoryDiagnoser]
public class QueryTrackingBehavior
{
[Params(10)]
public int NumBlogs { get; set; }

[GlobalSetup]
public void Setup()
{
Console.WriteLine("Setting up database...");
using var context = new BloggingContext();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
BloggingContext.SeedData(NumBlogs, NumPostsPerBlog);
Console.WriteLine("Setup complete.");
}
[Params(20)]
public int NumPostsPerBlog { get; set; }

[Benchmark(Baseline = true)]
public List<Post> AsTracking()
{
using var context = new BloggingContext();
[GlobalSetup]
public void Setup()
{
Console.WriteLine("Setting up database...");
using var context = new BloggingContext();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
BloggingContext.SeedData(NumBlogs, NumPostsPerBlog);
Console.WriteLine("Setup complete.");
}

return context.Posts.AsTracking().Include(p => p.Blog).ToList();
}
[Benchmark(Baseline = true)]
public List<Post> AsTracking()
{
using var context = new BloggingContext();

[Benchmark]
public List<Post> AsNoTracking()
{
using var context = new BloggingContext();
return context.Posts.AsTracking().Include(p => p.Blog).ToList();
}

return context.Posts.AsNoTracking().Include(p => p.Blog).ToList();
}
[Benchmark]
public List<Post> AsNoTracking()
{
using var context = new BloggingContext();

public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
return context.Posts.AsNoTracking().Include(p => p.Blog).ToList();
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True");
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }

public static void SeedData(int numBlogs, int numPostsPerBlog)
{
using var context = new BloggingContext();
context.AddRange(
Enumerable.Range(0, numBlogs).Select(
_ => new Blog { Posts = Enumerable.Range(0, numPostsPerBlog).Select(_ => new Post()).ToList() }));
context.SaveChanges();
}
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True");

public class Blog
public static void SeedData(int numBlogs, int numPostsPerBlog)
{
public int BlogId { get; set; }
public string Url { get; set; }
public int Rating { get; set; }
public List<Post> Posts { get; set; }
using var context = new BloggingContext();
context.AddRange(
Enumerable.Range(0, numBlogs).Select(
_ => new Blog { Posts = Enumerable.Range(0, numPostsPerBlog).Select(_ => new Post()).ToList() }));
context.SaveChanges();
}
}

public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public int Rating { get; set; }
public List<Post> Posts { get; set; }
}

public int BlogId { get; set; }
public Blog Blog { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }

public int BlogId { get; set; }
public Blog Blog { get; set; }
}
}
}
Loading

0 comments on commit a2b61f8

Please sign in to comment.