Skip to content

Instantly share code, notes, and snippets.

@roji
Created February 29, 2024 20:45
Show Gist options
  • Save roji/7762062a1726819fb5560d46b3cf66db to your computer and use it in GitHub Desktop.
Save roji/7762062a1726819fb5560d46b3cf66db to your computer and use it in GitHub Desktop.
await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
=> configurationBuilder.Properties<ReadOnlyMemory<byte>>().HaveConversion<ReadOnlyMemoryConverter>();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
private class ReadOnlyMemoryConverter() : ValueConverter<ReadOnlyMemory<byte>, byte[]>(v => v.ToArray(), v => v);
}
public class Blog
{
public int Id { get; set; }
public ReadOnlyMemory<byte> Bytes { get; set; }
}
@roji
Copy link
Author

roji commented Mar 1, 2024

Is it on by default? Or do I have to do something?

It's enabled by default - have you seen some other behavior?

Does it allow queries comparing dates?

No, because comparing the string representations of DateTimeOffset doesn't work - imagine having different time zones there, and then comparing string representations. If you're only storing timestamps in a single timezone (e.g. UTC), you can just use DateTime, which has a default value where comparison is supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment