Skip to content

Instantly share code, notes, and snippets.

@seankearon
Last active September 7, 2016 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seankearon/d315c7304eb8879e3f2a192482766bac to your computer and use it in GitHub Desktop.
Save seankearon/d315c7304eb8879e3f2a192482766bac to your computer and use it in GitHub Desktop.
Marten Queries
using System;
using System.Collections.Generic;
using System.Linq;
using Marten;
using Xunit;
namespace marten_test
{
public class CreateAndQuery
{
[Fact]
public void QueryLinq()
{
using (var store = DocumentStore.For("host=localhost;database=sk1;password=sean;username=postgres"))
using (var session = store.LightweightSession())
{
var u = session.Query<User>().First(x => x.Address.Line1.Contains("77"));
Assert.Equal("Darth", u.FirstName);
}
}
[Fact]
public void QueryText()
{
using (var store = DocumentStore.For("host=localhost;database=sk1;password=sean;username=postgres"))
using (var session = store.LightweightSession())
{
var value = session.Query<User>("select data from mt_doc_user where data->'Address'->>'Line1' = '7 High Street'").First();
Assert.Equal("Han", value.FirstName);
}
}
[Fact]
public void Create()
{
using (var store = DocumentStore.For("host=localhost;database=sk1;password=sean;username=postgres"))
using (var session = store.LightweightSession())
{
var user1 = new User
{
FirstName = "Han", LastName = "Solo",
Address = new Address {Line1 = "7 High Street", Town = "London"},
PreviousAddresses =
{
new Address {Line1 = "6 High Street", Town = "Liverpool"},
new Address {Line1 = "67 Fore Street", Town = "Troon"}
}
};
var user2 = new User
{
FirstName = "Darth", LastName = "Vader",
Address = new Address {Line1 = "77 High Street", Town = "Londinium"},
PreviousAddresses =
{
new Address {Line1 = "66 High Street", Town = "Liverpoole"},
new Address {Line1 = "77 Fore Street", Town = "Troonton"}
}
};
session.Store(user1, user2);
session.SaveChanges();
}
}
}
public class User
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Internal { get; set; }
public string UserName { get; set; }
public Address Address { get; set; }
public List<Address> PreviousAddresses { get; set; }
public User()
{
PreviousAddresses = new List<Address>();
}
}
public class Address
{
public string Line1 { get; set; }
public string Town { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment