SapphireDb logo SapphireDb

Get started - Setup server

SapphireDb-Server runs on an Asp.Net Core application. First step is to setup the server to use SapphireDb.
You need to have a working Asp.Net Core application and a configured DbContext.

Installation

Install package

To install the package execute the following command in your package manager console

You can also install the extension using Nuget package manager. The project can be found here: https://www.nuget.org/packages/SapphireDb/

Configure DbContext

You now have to change your DbContext to derive from SapphireDbContext. Also make sure to adjust the constructor parameters.

\f:(csharp:MyDbContext.cs) // Change DbContext to SapphireDbContext\n public class MyDbContext : SapphireDbContext\n {\n \t//Add SapphireDatabaseNotifier for DI\n \tpublic MyDbContext(DbContextOptions<MyDbContext> options, SapphireDatabaseNotifier notifier) : base(options, notifier)\n \t{\n\n \t}\n\n \tpublic DbSet<User> Users { get; set; }\n\n \tpublic DbSet<Test> Tests { get; set; }\n }

Register services and update pipeline

To use the SapphireDb you also have to make some changes in your Startup.cs-File.

\f:(csharp:Startup.cs) public class Startup\n {\n \tpublic void ConfigureServices(IServiceCollection services)\n \t{\n \t\t//Register services\n \t\tservices.AddSapphireDb(...)\n \t\t\t.AddContext<MyDbContext>(cfg => ...);\n \t}\n\n \tpublic void Configure(IApplicationBuilder app)\n \t{\n \t\t//Add Middleware\n \t\tapp.UseSapphireDb();\n \t}\n }

(Optional) Enable CORS

If you are using a client application that has a different origin than the server application you have to enable CORS

\f:(csharp:Startup.cs) public class Startup\n {\n \tpublic void Configure(IApplicationBuilder app)\n \t{\n \t\t//Add Middleware\n \t\tapp.UseCors(cfg => cfg.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());\n \t}\n }

Example

You can also check out the example project: Example

Done

Your SapphireDb-server should now work.

Continue with the setup of a client

This website uses Google Analytics. If you want to disable it just unselect the option: