Created
February 13, 2023 20:22
-
-
Save khalidabuhakmeh/f2d7a8b2b5bbad89bbc9ef1dbd78c769 to your computer and use it in GitHub Desktop.
Marten Query with Enum Value
This file contains 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
// 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