Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement