Skip to content

Instantly share code, notes, and snippets.

@AKlaus
Created August 1, 2019 01: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 AKlaus/44a6849ccf0dcea4e8a6fa81bbc7ebb7 to your computer and use it in GitHub Desktop.
Save AKlaus/44a6849ccf0dcea4e8a6fa81bbc7ebb7 to your computer and use it in GitHub Desktop.
using Raven.Client.Documents;
using Raven.Client.Documents.Commands.Batches;
using Raven.Client.Documents.Operations;
using Raven.Client.Documents.Queries;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
public class Person
{
public string Id { get; set; }
public string Text { get; set; }
public List<UserChange> Modifications { get; set; }
public class UserChange
{
public string Id { get; set; }
public DateTime Timestamp { get; set; }
}
}
static void Main()
{
MainAsync().Wait();
}
static async Task MainAsync()
{
using (var store = new DocumentStore
{
Urls = new[] { "http://live-test.ravendb.net" },
Database = "test"
})
{
store.Initialize();
#region Delete all documents in the DB --------------
var operation = await store.Operations.SendAsync(new DeleteByQueryOperation(
new IndexQuery { Query = "from @all_docs as d" },
new QueryOperationOptions
{
RetrieveDetails = true
}
));
await operation.WaitForCompletionAsync<BulkOperationResult>();
#endregion ------------------------------------------
// The document ID for future patching
string personId = null;
using (var s = store.OpenAsyncSession())
{
#region Seed data -----------------------------------
for (var i = 1; i < 3; i++)
{
var p = new Person
{
Modifications = new List<Person.UserChange>
{
new Person.UserChange { Id = "" + i, Timestamp = DateTime.Now }
}
};
await s.StoreAsync(p);
personId = p.Id;
}
await s.SaveChangesAsync();
#endregion ------------------------------------------
s.Advanced.Defer(
new PatchCommandData(
personId,
null,
new PatchRequest { Script = $@"this.Text = 'aaa';" },
null));
Person person = await s.LoadAsync<Person>(personId);
Console.WriteLine($"Text = '{person.Text}' (expected ''). UserChange.Count = {person.Modifications.Count} (expected 1)");
await s.SaveChangesAsync();
// BUG: 'Modifications' collection has 2 records. Why?
person = await s.LoadAsync<Person>(personId);
Console.WriteLine($"Text = '{person.Text}' (expected 'aaa'). UserChange.Count = {person.Modifications.Count} (expected 1)");
}
Console.WriteLine("The end!");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment