Skip to content

Instantly share code, notes, and snippets.

@khalidabuhakmeh
Created February 13, 2023 20:22
Show Gist options
  • Save khalidabuhakmeh/f2d7a8b2b5bbad89bbc9ef1dbd78c769 to your computer and use it in GitHub Desktop.
Save khalidabuhakmeh/f2d7a8b2b5bbad89bbc9ef1dbd78c769 to your computer and use it in GitHub Desktop.
Marten Query with Enum Value
// See https://aka.ms/new-console-template for more information
using Marten;
using Weasel.Core;
async Task StoreUser(DocumentStore documentStore)
{
await using var session = documentStore.OpenSession();
var newUser = new User
{
UserName = "khalid.abuhakmeh",
Role = Roles.Admin
};
session.Store(newUser);
await session.SaveChangesAsync();
}
using var store = DocumentStore.For(cfg =>
{
cfg.Connection("Server=127.0.0.1;Port=5432;Database=marten;User Id=postgres;Password=Pass123!;");
cfg.AutoCreateSchemaObjects = AutoCreate.All;
});
await StoreUser(store);
await using var session = store.LightweightSession();
var user = session
.Query<User>()
.First(u => u.Role == Roles.Admin);
Console.WriteLine($"{user.UserName} is an {user.Role}");
public class User
{
public int Id { get; set; }
public Roles Role { get; set; }
public string UserName { get; set; } = "";
}
public enum Roles
{
User,
Admin
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment