Last active
January 31, 2022 00:24
-
-
Save gistlyn/a1832fe90132bc46b27bf7281ad71b13 to your computer and use it in GitHub Desktop.
Use RavenDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dotnet add package RavenDB.Client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated, thanks @ziykon !