Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active June 22, 2019 21:16
Show Gist options
  • Save gistlyn/8ef41d53c8f54d85a50f26e2aad6da73 to your computer and use it in GitHub Desktop.
Save gistlyn/8ef41d53c8f54d85a50f26e2aad6da73 to your computer and use it in GitHub Desktop.
Use RavenDB
dotnet add package RavenDB.Client
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServiceStack;
using ServiceStack.DataAnnotations;
using Raven.Client.Documents;
namespace MyApp
{
public class ConfigureRavenDb : IConfigureServices
{
IConfiguration Configuration { get; }
public ConfigureRavenDb(IConfiguration configuration) => Configuration = configuration;
public void Configure(IServiceCollection services)
{
var store = new DocumentStore
{
Urls = new[] // URL to the Server, or list of URLs to all Cluster Servers (Nodes)
{
Configuration.GetConnectionString("RavenDB") ?? "http://localhost:8080"
},
Database = "MyApp", // Default database that DocumentStore will interact with
Conventions = { } // DocumentStore customizations
};
store.Conventions.FindIdentityProperty = p => {
var attr = p.DeclaringType.FirstAttribute<IndexAttribute>(); // Allow overriding 'Id' Identity property
return attr != null
? p.Name == attr.Name
: p.Name == "Id";
};
services.AddSingleton<IDocumentStore>(store.Initialize());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment