Skip to content

Instantly share code, notes, and snippets.

@brianmed
Created December 22, 2021 18:47
Show Gist options
  • Save brianmed/e2c776b8f4a92cb80c07bd76eb00848d to your computer and use it in GitHub Desktop.
Save brianmed/e2c776b8f4a92cb80c07bd76eb00848d to your computer and use it in GitHub Desktop.
SQLite Example in C# that Creates Rows with NewId and Random Data via DbContext
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata;
using MassTransit;
Console.WriteLine($"Inserting {args[0]} records");
JoyDbContext joyDbContext = new();
joyDbContext.Database.EnsureCreated();
joyDbContext.Database.ExecuteSqlRaw("PRAGMA journal_mode=WAL;");
Random random = new();
int start = (int)Math.Max(1, joyDbContext.Joy
.Select(v => v.JoyId).DefaultIfEmpty().Max());
foreach (int idx in Enumerable.Range(start, Int32.Parse(args[0])))
{
joyDbContext.Add(new JoyEntity
{
Uuid = NewId.Next().ToString(),
ExampleData = Convert.ToBase64String(
BitConverter.GetBytes(random.Next(Int32.MaxValue)))
});
if (idx % 300 == 0) {
Console.WriteLine($"{DateTime.Now.ToString("s")}: {idx}");
joyDbContext.SaveChangesAsync();
joyDbContext.ChangeTracker.Clear();
}
}
[Index(nameof(Uuid), IsUnique = true)]
public class JoyEntity
{
[Key]
public long JoyId { get; set; }
[Required]
public string Uuid { get; set; }
[Required]
public string ExampleData { get; set; }
[DefaultValue(typeof(DateTime), "")]
public DateTime Updated { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime Inserted { get; set; } = DateTime.UtcNow;
}
public class JoyDbContext : DbContext
{
public DbSet<JoyEntity> Joy { get; set; }
public JoyDbContext()
{
}
public JoyDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options
.UseSqlite($"Data Source=joy.sqlite");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
foreach (IMutableForeignKey fk in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
fk.DeleteBehavior = DeleteBehavior.Restrict;
}
}
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
IEnumerable<EntityEntry> entries = ChangeTracker
.Entries()
.Where(e => e.State == EntityState.Modified);
foreach (EntityEntry entityEntry in entries)
{
entityEntry.Entity.GetType().GetProperty("Updated")?.SetValue(entityEntry.Entity, DateTime.UtcNow);
}
return base.SaveChangesAsync(cancellationToken);
}
public override int SaveChanges()
{
IEnumerable<EntityEntry> entries = ChangeTracker
.Entries()
.Where(e => e.State == EntityState.Modified);
foreach (EntityEntry entityEntry in entries)
{
entityEntry.Entity.GetType().GetProperty("Updated")?.SetValue(entityEntry.Entity, DateTime.UtcNow);
}
return base.SaveChanges();
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.1" />
<PackageReference Include="NewId" Version="3.0.3" />
</ItemGroup>
</Project>
$ time dotnet run 15000000
...
dotnet run 15000000 965.22s user 753.46s system 70% cpu 40:33.50 total
$ ls -lFa -h joy.sqlite
-rw-r--r-- 1 bpm staff 2.2G Dec 22 13:44 joy.sqlite
$ sqlite3 joy.sqlite <<< 'select count(JoyId) from Joy;'
15000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment