GithubHelp home page GithubHelp logo

motycak / kros.korm.extensions.asp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kros-sk/kros.korm.extensions.asp

0.0 1.0 0.0 85 KB

For simple integration into ASP.NET Core projects.

License: MIT License

C# 100.00%

kros.korm.extensions.asp's Introduction

Kros.KORM.Extensions.Asp Build Status

For simple integration into ASP.NET Core projects, the Kros.KORM.Extensions.Asp package was created.

Documentation

For configuration, general information and examples see the documentation.

Download

Kros.KORM.Extensions.Asp is available from Nuget Kros.KORM.Extensions.Asp.

Contributing Guide

To contribute with new topics/information or make changes, see contributing for instructions and guidelines.

This topic contains following sections

ASP.NET Core Extensions

You can use the AddKorm extension methods to register databases to the DI container. This registers IDatabaseFactory into DI container. This factory can be used to retrieve IDatabase instances by name. If no name is specified, default name DefaultConnection will be used. IDatabase instances has scoped lifetime.

The first database registered by AddKorm method is also added to the DI container directly as IDatabase dependency. This is for simple use case, when only one database is used. So there is no need for using IDatabaseFactory.

public void ConfigureServices(IServiceCollection services)
{
    services.AddKorm(Configuration);
}

The configuration file (typically appsettings.json) must contain a standard connection strings section (ConnectionStrings). Name of the connection string can be specified as second parameter. If the name is not specified, default name DefaultConnection will be used.

"ConnectionStrings": {
  "DefaultConnection": "Server=ServerName\\InstanceName; Initial Catalog=database; Integrated Security=true",
  "localConnection": "Server=Server2\\Instance; Integrated Security=true;"
}

Connection string can be passed directly to AddKorm method, together with its name. The name DefaultConnection will be used if no name is specified.

public void ConfigureServices(IServiceCollection services)
{
    // Added from appsettings.json under "localConnection" name.
    services.AddKorm(Configuration, "localConnection");

    // Added directly with the name "db2".
    services.AddKorm("Server=ServerName\\InstanceName; Initial Catalog=database; Integrated Security=true", "db2");
}

KORM supports additional settings for connections:

  • AutoMigrate: The value is boolean true/false. If not set (or the value is invalid), the default value is false. If it is true, it allows automatic database migrations.
  • KormProvider: This specifies database provider which will be used. If not set, the value System.Data.SqlClient will be used. KORM currently supports only Microsoft SQL Server, so there is no need to use this parameter.

These settings (if needed) can also be set in configuration file under the KormSettings section. Settings are identified by connection string name.

"KormSettings": {
  "DefaultConnection": {
    "AutoMigrate": true
  },
  "localConnection": {
    "AutoMigrate": true
  }
}

Database Migrations

For simple database migration, you must call:

public void ConfigureServices(IServiceCollection services)
{
    services.AddKorm(Configuration)
        .AddKormMigrations()
        .Migrate();
}

Migrations are disabled by default, so the previous code requires that the automatic migrations are enabled in connection string: KormAutoMigrate=true

Korm by default performs migrations by searching the main assembly for files in SqlScripts directory. The script file name must match pattern {migrationId}_{MigrationName}.sql. MigrationId is increasing number over time.

For example: 20190301001_AddPeopleTable.sql

CREATE TABLE [dbo].People (
    [Id] [int] NOT NULL,
    [Name] [nvarchar](255)
CONSTRAINT [PK_People] PRIMARY KEY CLUSTERED ([Id] ASC)
    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Migration can also be executed through an HTTP request. By calling the /kormmigration endpoint, the necessary migrations will be executed. However, you need to add middleware:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseKormMigrations();
}

You can change the endpoint URL by configuration:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseKormMigrations(options =>
    {
        options.EndpointUrl = "/loremipsum";
    });
}

If multiple KORM databases are registered, all of them have unique name. Migrations are performed per database and the name of the database is specified in URL as another path segment: /kormmigration/dbname If the name is not specified, default connection string will be used (DefaultConnection).

If you have scripts stored in a different way (for example, somewhere on a disk or in another assembly), you can configure your own providers to get these scripts.

public void ConfigureServices(IServiceCollection services)
{
    services.AddKorm(Configuration)
        .AddKormMigrations(o =>
        {
            var assembly = AppDomain.CurrentDomain.GetAssemblies()    .FirstOrDefault(x => x.FullName.StartsWith  ("Demo.DatabaseLayer")  );
            o.AddAssemblyScriptsProvider(assembly,     "Demo.DatabaseLayer.Resources");
            o.AddFileScriptsProvider(@"C:\scripts\");
            o.AddScriptsProvider(new MyCustomScriptsProvider());
        })
        .Migrate();
}

KORM creates a __KormMigrationsHistory table in which it has a history of individual migrations.

Id Generators

If you need to initialize the database for IIdGenerator then you can call InitDatabaseForIdGenerator.

public void ConfigureServices(IServiceCollection services)
{
    services.AddKorm(Configuration)
        .InitDatabaseForIdGenerator();
}

kros.korm.extensions.asp's People

Contributors

burgyn avatar lukassefcik avatar motycak avatar satano avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.