Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use file-scoped namespaces everywhere in samples #3708

Merged
merged 1 commit into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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