{"id":21654612,"url":"https://github.com/kros-sk/kros.korm.extensions.asp","last_synced_at":"2026-03-11T03:31:47.341Z","repository":{"id":42973666,"uuid":"177153592","full_name":"Kros-sk/Kros.KORM.Extensions.Asp","owner":"Kros-sk","description":"For simple integration into ASP.NET Core projects.","archived":false,"fork":false,"pushed_at":"2025-02-04T07:23:32.000Z","size":114,"stargazers_count":1,"open_issues_count":2,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-21T20:48:42.909Z","etag":null,"topics":["dot-net","dotnet","orm","orm-library"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Kros-sk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-03-22T14:20:59.000Z","updated_at":"2025-02-04T07:22:06.000Z","dependencies_parsed_at":"2025-04-11T21:13:50.034Z","dependency_job_id":"d4985256-80fa-4f89-9e78-01ec195f29fd","html_url":"https://github.com/Kros-sk/Kros.KORM.Extensions.Asp","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/Kros-sk/Kros.KORM.Extensions.Asp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kros-sk%2FKros.KORM.Extensions.Asp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kros-sk%2FKros.KORM.Extensions.Asp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kros-sk%2FKros.KORM.Extensions.Asp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kros-sk%2FKros.KORM.Extensions.Asp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kros-sk","download_url":"https://codeload.github.com/Kros-sk/Kros.KORM.Extensions.Asp/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kros-sk%2FKros.KORM.Extensions.Asp/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263833416,"owners_count":23517373,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dot-net","dotnet","orm","orm-library"],"created_at":"2024-11-25T08:28:26.424Z","updated_at":"2026-03-11T03:31:47.309Z","avatar_url":"https://github.com/Kros-sk.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Kros.KORM.Extensions.Asp [![Build Status](https://dev.azure.com/krossk/DevShared/_apis/build/status/Kros.KORM.Extensions.Asp/Kros.KORM.Extensions.Asp?branchName=features/build)](https://dev.azure.com/krossk/DevShared/_build/latest?definitionId=69\u0026branchName=master)\n\nFor simple integration into ASP.NET Core projects, the [__Kros.KORM.Extensions.Asp__](https://www.nuget.org/packages/Kros.KORM.Extensions.Asp/) package was created.\n\n## Documentation\n\nFor configuration, general information and examples [see the documentation](https://kros-sk.github.io/docs/Kros.KORM.Extensions.Asp/).\n\n### Download\n\nKros.KORM.Extensions.Asp is available from __Nuget__ [__Kros.KORM.Extensions.Asp__](https://www.nuget.org/packages/Kros.KORM.Extensions.Asp/).\n\n## Contributing Guide\n\nTo contribute with new topics/information or make changes, see [contributing](https://github.com/Kros-sk/Kros.KORM.Extensions.Asp/blob/master/CONTRIBUTING.md) for instructions and guidelines.\n\n## This topic contains following sections\n\n* [ASP.NET Core Extensions](#aspnet-core-extensions)\n* [Database Migrations](#database-migrations)\n* [Id Generators](#id-generators)\n* [Converters](#converters)\n\n### ASP.NET Core Extensions\n\nYou 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.\n\n**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`.\n\n``` csharp\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddKorm(Configuration);\n}\n```\n\nThe 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.\n\n``` json\n\"ConnectionStrings\": {\n  \"DefaultConnection\": \"Server=ServerName\\\\InstanceName; Initial Catalog=database; Integrated Security=true\",\n  \"localConnection\": \"Server=Server2\\\\Instance; Integrated Security=true;\"\n}\n```\n\nConnection string can be passed directly to `AddKorm` method, together with its name. The name `DefaultConnection` will be used if no name is specified.\n\n``` csharp\npublic void ConfigureServices(IServiceCollection services)\n{\n    // Added from appsettings.json under \"localConnection\" name.\n    services.AddKorm(Configuration, \"localConnection\");\n\n    // Added directly with the name \"db2\".\n    services.AddKorm(\"Server=ServerName\\\\InstanceName; Initial Catalog=database; Integrated Security=true\", \"db2\");\n}\n```\n\nKORM supports additional settings for connections:\n\n* `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](#database-migrations).\n* `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.\n\nThese settings (if needed) can also be set in configuration file under the `KormSettings` section. Settings are identified by connection string name.\n\n``` json\n\"KormSettings\": {\n  \"DefaultConnection\": {\n    \"AutoMigrate\": true\n  },\n  \"localConnection\": {\n    \"AutoMigrate\": true\n  }\n}\n```\n\n### Database Migrations\n\nFor simple database migration, you must call:\n\n``` csharp\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddKorm(Configuration)\n        .AddKormMigrations()\n        .Migrate();\n}\n```\n\nMigrations are disabled by default, so the previous code requires that the automatic migrations are enabled in KormSettings for each connection string: `AutoMigrate=true`\n\nKorm 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.\n\nFor example: `20190301001_AddPeopleTable.sql`\n\n``` sql\nCREATE TABLE [dbo].People (\n    [Id] [int] NOT NULL,\n    [Name] [nvarchar](255)\nCONSTRAINT [PK_People] PRIMARY KEY CLUSTERED ([Id] ASC)\n    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]\n) ON [PRIMARY]\nGO\n```\n\nMigration 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:\n\n``` csharp\npublic void Configure(IApplicationBuilder app, IHostingEnvironment env)\n{\n    app.UseKormMigrations();\n}\n```\n\nYou can change the endpoint URL by configuration:\n\n``` csharp\npublic void Configure(IApplicationBuilder app, IHostingEnvironment env)\n{\n    app.UseKormMigrations(options =\u003e\n    {\n        options.EndpointUrl = \"/loremipsum\";\n    });\n}\n```\n\nIf 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`).\n\nIf 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.\n\n``` csharp\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddKorm(Configuration)\n        .AddKormMigrations(o =\u003e\n        {\n            var assembly = AppDomain.CurrentDomain.GetAssemblies()    .FirstOrDefault(x =\u003e x.FullName.StartsWith  (\"Demo.DatabaseLayer\")  );\n            o.AddAssemblyScriptsProvider(assembly,     \"Demo.DatabaseLayer.Resources\");\n            o.AddFileScriptsProvider(@\"C:\\scripts\\\");\n            o.AddScriptsProvider(new MyCustomScriptsProvider());\n        })\n        .Migrate();\n}\n```\n\nIt is possible to set your own timeout for running migration script. Defaul timeout is 30 seconds.\n\n``` csharp\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddKorm(Configuration)\n        .AddKormMigrations(o =\u003e\n        {\n            o.Timeout = TimeSpan.FromMinutes(1);\n        })\n        .Migrate();\n}\n```\n\nKORM creates a `__KormMigrationsHistory` table in which it has a history of individual migrations.\n\n### Id Generators\n\nIf you need to initialize the database for [IIdGenerator](https://kros-sk.github.io/Kros.Libs.Documentation/api/Kros.Utils/Kros.Data.IIdGenerator.html) then you can call `InitDatabaseForIdGenerator`.\n\n``` csharp\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddKorm(Configuration)\n        .InitDatabaseForIdGenerator();\n}\n```\n\n### Converters\n\nKORM supports custom converters (via [IConverter](https://kros-sk.github.io/Kros.Libs.Documentation/api/Kros.KORM/Kros.KORM.Converter.IConverter.html) implementation) for converting values from DB to objects and vice versa. These converters are set for individual columns in `OnModelCreating` method of database configuration class (`DatabaseConfigurationBase`).\n\nIf you need to convert text formatted as JSON from database to entity, you can use pre-defined converter - `JsonConverter`:\n\n``` csharp\npublic override void OnModelCreating(ModelConfigurationBuilder modelBuilder)\n{\n    modelBuilder.Entity\u003cMyEntity\u003e()\n        .HasTableName(MyEntityTableName)\n        .Property(x =\u003e x.MySerializableProperty).UseConverter\u003cJsonConverter\u003e();\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkros-sk%2Fkros.korm.extensions.asp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkros-sk%2Fkros.korm.extensions.asp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkros-sk%2Fkros.korm.extensions.asp/lists"}