Skip to content

Instantly share code, notes, and snippets.

@joelverhagen
Last active January 5, 2019 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelverhagen/88560062766acbe8adc290c37fea0b23 to your computer and use it in GitHub Desktop.
Save joelverhagen/88560062766acbe8adc290c37fea0b23 to your computer and use it in GitHub Desktop.
FirstAsync() works, Take(1).ToListAsync() times out, Take(1).FirstAsync() throws
Starting...
Success in 00:00:01.7050145.
Starting...
Exception in 00:00:30.1420518: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Starting...
Exception in 00:00:00.0449286: Invalid column name 'c'.
Starting...
Success in 00:00:00.4171223.
Starting...
Success in 00:00:00.0271643.
Starting...
Exception in 00:00:00.0269980: Invalid column name 'c'.
using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
MainAsync(args).GetAwaiter().GetResult();
}
static async Task MainAsync(string[] args)
{
var connectionString =
"Data Source=(localdb)\\mssqllocaldb; Initial Catalog=ReproForJoel; Integrated Security=True; MultipleActiveResultSets=True";
using (var entities = new SqlServerEntityContext(connectionString))
{
await entities.Database.EnsureCreatedAsync();
if (!await entities.CatalogPackages.AnyAsync())
{
entities.CatalogPackages.Add(new CatalogPackage
{
LastCommitTimestamp = 2,
Package = new Package
{
PackageRegistration = new PackageRegistration
{
},
}
});
await entities.SaveChangesAsync();
}
}
await ExecuteAsync(connectionString, q => q.FirstAsync());
await ExecuteAsync(connectionString, q => q.Take(1).ToListAsync());
await ExecuteAsync(connectionString, q => q.Take(1).FirstAsync());
}
private static async Task ExecuteAsync(string connectionString, Func<IQueryable<Package>, Task> execute)
{
var start = 1;
var end = 10;
using (var entities = new SqlServerEntityContext(connectionString))
{
var stopwatch = Stopwatch.StartNew();
try
{
var query = entities
.Packages
.Include(x => x.PackageRegistration)
.Include(x => x.CatalogPackage)
.Where(x => x.CatalogPackage != null)
.Where(x => x.CatalogPackage.LastCommitTimestamp > start && x.CatalogPackage.LastCommitTimestamp <= end)
.OrderBy(x => x.CatalogPackage.LastCommitTimestamp);
Console.WriteLine("Starting...");
await execute(query);
Console.WriteLine($"Success in {stopwatch.Elapsed}.");
}
catch (Exception ex)
{
Console.WriteLine($"Exception in {stopwatch.Elapsed}: {ex.Message}");
}
}
}
}
public class SqlServerEntityContext : DbContext
{
private readonly string _connectionString;
public SqlServerEntityContext(string connectionString)
{
_connectionString = connectionString;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(_connectionString)
// .UseLoggerFactory(new LoggerFactory().AddConsole(LogLevel.Trace))
;
}
public DbSet<PackageRegistration> PackageRegistrations { get; set; }
public DbSet<Package> Packages { get; set; }
public DbSet<CatalogPackage> CatalogPackages { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<CatalogPackage>()
.HasOne(x => x.Package)
.WithOne(x => x.CatalogPackage)
.HasForeignKey<CatalogPackage>(x => x.PackageKey);
}
}
public class PackageRegistration
{
[Key]
public long PackageRegistrationKey { get; set; }
public string Id { get; set; }
}
public class Package
{
public long PackageRegistrationKey { get; set; }
[Key]
public long PackageKey { get; set; }
public string Version { get; set; }
public string Identity { get; set; }
public PackageRegistration PackageRegistration { get; set; }
public CatalogPackage CatalogPackage { get; set; }
}
public class CatalogPackage
{
[Key]
public long PackageKey { get; set; }
public bool Deleted { get; set; }
public long FirstCommitTimestamp { get; set; }
public long LastCommitTimestamp { get; set; }
public bool Listed { get; set; }
public Package Package { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment