Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active January 31, 2022 00:24
Show Gist options
  • Save gistlyn/a1832fe90132bc46b27bf7281ad71b13 to your computer and use it in GitHub Desktop.
Save gistlyn/a1832fe90132bc46b27bf7281ad71b13 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;
[assembly: HostingStartup(typeof(MyApp.ConfigureRavenDb))]
namespace MyApp
{
public class ConfigureRavenDb : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices((context, services) => {
var store = new DocumentStore
{
Urls = new[] // URL to the Server, or list of URLs to all Cluster Servers (Nodes)
{
context.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());
});
}
}
@Layoric
Copy link

Layoric commented Jan 31, 2022

Updated, thanks @ziykon !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment