using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

using var db = new MyContext();
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
db.AddRange(
	new Job { Name = ".NET Developer Days", State = State.Running },
	new Job { Name = "EF Core 6 Talk", State = State.Unknown },
	new Job { Name = "Hope Not This Demo", State = State.Failed });
db.SaveChanges();
// use this feature with caution
db.Set<Job>().Where(x => x.State == State.Unknown).ToList();

public class MyContext : DbContext
{
	protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
	{
		base.OnConfiguring(optionsBuilder);
		if (optionsBuilder.IsConfigured)
			return;
		optionsBuilder.UseSqlServer(@"Server=.;Database=netdd;Trusted_Connection=true;ConnectRetryCount=0");
		optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuting });
		optionsBuilder.EnableSensitiveDataLogging();
	}

	protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		base.OnModelCreating(modelBuilder);

		modelBuilder.Entity<Job>()
			.Property(x => x.State)
			.HasConversion<StateConverter>();
	}
}

public class Job
{
	public Guid Id { get; set; }
	public string Name { get; set; } = null!;
	public State? State { get; set; }
}

public enum State
{
	Unknown,
	Running,
	Failed,
}

public class StateConverter : ValueConverter<State, string?>
{
	public StateConverter()
		: base(
			v => v == State.Unknown ? null : v.ToString(),
			v => v == null ? State.Unknown : Enum.Parse<State>(v),
			convertsNulls: true)
	{ }
}