-
-
Save stormwild/e172330a19c366cef72c92b84a6bbb27 to your computer and use it in GitHub Desktop.
Storing `IJobStorageRecord` and `IEventStorageRecord` via EntityFramework Core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using FastEndpoints; | |
using MessagePack; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | |
public class JobRecord : IJobStorageRecord | |
{ | |
public Guid Id { get; set; } | |
public string QueueID { get; set; } | |
public object Command { get; set; } | |
public DateTime ExecuteAfter { get; set; } | |
public DateTime ExpireOn { get; set; } | |
public bool IsComplete { get; set; } | |
} | |
public class MyDB : DbContext | |
{ | |
public DbSet<JobRecord> Jobs { get; set; } | |
protected override void OnConfiguring(DbContextOptionsBuilder o) | |
=> o.UseSqlite("Data Source=JobDatabase"); | |
protected override void OnModelCreating(ModelBuilder b) | |
{ | |
MessagePackSerializer.DefaultOptions | |
= MessagePack.Resolvers.ContractlessStandardResolver.Options; | |
var converter = new ValueConverter<object, byte[]>( | |
v => Serialize(v), | |
v => Deserialize(v)); | |
b.Entity<JobRecord>() | |
.Property(e => e.Command) | |
.HasConversion(converter) | |
.HasColumnType("BLOB"); //VARBINARY for sql server | |
} | |
static byte[] Serialize(object cmd) => MessagePackSerializer.Serialize(cmd); | |
static object Deserialize(byte[] cmdBytes) => MessagePackSerializer.Deserialize<object>(cmdBytes); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment