{"id":15410349,"url":"https://github.com/msallin/sqlitecodefirst","last_synced_at":"2025-05-15T03:03:16.441Z","repository":{"id":29161040,"uuid":"32691474","full_name":"msallin/SQLiteCodeFirst","owner":"msallin","description":"Creates a SQLite Database based on a EdmModel by using Entity Framework CodeFirst.","archived":false,"fork":false,"pushed_at":"2024-02-23T12:39:34.000Z","size":1048,"stargazers_count":617,"open_issues_count":4,"forks_count":122,"subscribers_count":40,"default_branch":"master","last_synced_at":"2025-04-14T00:58:54.417Z","etag":null,"topics":["codefirst","csharp","entity-framework","sqlite","sqlite-codefirst"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"shenzhe/ant","license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/msallin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":"msallin"}},"created_at":"2015-03-22T19:30:21.000Z","updated_at":"2025-03-04T10:19:31.000Z","dependencies_parsed_at":"2024-02-23T12:49:43.491Z","dependency_job_id":null,"html_url":"https://github.com/msallin/SQLiteCodeFirst","commit_stats":{"total_commits":241,"total_committers":22,"mean_commits":"10.954545454545455","dds":0.3817427385892116,"last_synced_commit":"14d91e749ea762f8679aa345a74f66a4a60e6631"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msallin%2FSQLiteCodeFirst","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msallin%2FSQLiteCodeFirst/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msallin%2FSQLiteCodeFirst/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msallin%2FSQLiteCodeFirst/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/msallin","download_url":"https://codeload.github.com/msallin/SQLiteCodeFirst/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248804790,"owners_count":21164132,"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":["codefirst","csharp","entity-framework","sqlite","sqlite-codefirst"],"created_at":"2024-10-01T16:44:07.940Z","updated_at":"2025-04-14T00:59:00.906Z","avatar_url":"https://github.com/msallin.png","language":"C#","readme":"# SQLite CodeFirst\n\n**Release Build** [![Build status](https://ci.appveyor.com/api/projects/status/2qavdqctw0ehscm6/branch/master?svg=true)](https://ci.appveyor.com/project/msallin/sqlitecodefirst-nv6vn/branch/master)\n\n**CI Build** [![Build status](https://ci.appveyor.com/api/projects/status/oc1miog385h801qe?svg=true)](https://ci.appveyor.com/project/msallin/sqlitecodefirst)\n\nCreates a [SQLite Database](https://sqlite.org/) from Code, using [Entity Framework](https://msdn.microsoft.com/en-us/data/ef.aspx) CodeFirst.\n\n## Support the project \u003ca href=\"https://www.paypal.com/cgi-bin/webscr?cmd=_donations\u0026business=ARTMHALNW4VC6\u0026lc=CH\u0026item_name=SQLite%2eCodeFirst\u0026item_number=sqlitecodefirst\u0026currency_code=CHF\u0026bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted\" title=\"Donate to this project using Paypal\"\u003e\u003cimg src=\"https://camo.githubusercontent.com/11b2f47d7b4af17ef3a803f57c37de3ac82ac039/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f70617970616c2d646f6e6174652d79656c6c6f772e737667\" alt=\"PayPal donate button\" data-canonical-src=\"https://img.shields.io/badge/paypal-donate-yellow.svg\" style=\"max-width:100%;\"\u003e\u003c/a\u003e\n\nTo support this project you can: *star the repository*, report bugs/request features by creating new issues, write code and create PRs or donate.\nEspecially if you use it for a commercial project, a donation is welcome.\nIf you need a specific feature for a commercial project, I am glad to offer a paid implementation.\n\n## Project Status\n\nThis project was started when there was .NET Full Framework and EF 6. EF 6 does not offer code first for SQLite and this library fills this gab.\nNowadays there is .NET Core (or now just called .NET) and EF Core. EF Core supports code first and migrations for SQLite.\nIf you use .NET Core 3 or above together with EF Core, there is no need for this library.\nIf you use EF 6 (either with .NET Full Framework or with .NET Core 3 or above), this library is an option for you to get code first for SQLite.\nI'm going to maintain this library as long as it is useful for some people (see [History](https://github.com/msallin/SQLiteCodeFirst/issues/166) \u0026 [Project Status and Release Schedule](https://github.com/msallin/SQLiteCodeFirst/issues/157)).\n\n## Features\n\nThis project ships several `IDbInitializer` classes. These create new SQLite Databases based on your model/code.\n\nThe following features are supported:\n\n- Tables from classes (supported annotations: `Table`)\n- Columns from properties (supported annotations: `Column`, `Key`, `MaxLength`, `Required`, `NotMapped`, `DatabaseGenerated`, `Index`)\n- PrimaryKey constraint (`Key` annotation, key composites are supported)\n- ForeignKey constraint (1-n relationships, support for 'Cascade on delete')\n- Not Null constraint\n- Auto increment (An int PrimaryKey will automatically be incremented and you can explicit set the \"AUTOINCREMENT\" constraint to a PrimaryKey using the Autoincrement-Attribute)\n- Index (Decorate columns with the `Index` attribute. Indices are automatically created for foreign keys by default. To prevent this you can remove the convention `ForeignKeyIndexConvention`)\n- Unique constraint (Decorate columns with the `UniqueAttribute`, which is part of this library)\n- Collate constraint (Decorate columns with the `CollateAttribute`, which is part of this library. Use `CollationFunction.Custom` to specify a own collation function.)\n- Default collation (pass an instance of Collation as constructor parameter for an initializer to specify a default collation).\n- SQL default value (Decorate columns with the `SqlDefaultValueAttribute`, which is part of this library)\n\n## Install\n\nEither get the assembly from the latest [GitHub Release Page](https://github.com/msallin/SQLiteCodeFirst/releases) or install the NuGet-Package [SQLite.CodeFirst](https://www.nuget.org/packages/SQLite.CodeFirst/) (`PM\u003e Install-Package SQLite.CodeFirst`).\n\nThe project is built to target .NET framework versions 4.0 and 4.5 and .NET Standard 2.1.\nYou can use the SQLite CodeFirst in projects that target the following frameworks:\n\n- .NET 4.0 (uses net40)\n- .NET 4.5-4.8 (uses net45)\n- .NET Core 3.0-3.1 (uses netstandard2.1)\n- .NET 5-8 (uses netstandard2.1)\n\n## How to use\n\nThe functionality is exposed by using implementations of the `IDbInitializer\u003c\u003e` interface.\nDepending on your need, you can choose from the following initializers:\n\n- SqliteCreateDatabaseIfNotExists\n- SqliteDropCreateDatabaseAlways\n- SqliteDropCreateDatabaseWhenModelChanges\n\nIf you want to have more control, you can use the `SqliteDatabaseCreator` (implements `IDatabaseCreator`) which lets you control the creation of the SQLite database.\nOr for even more control, use the `SqliteSqlGenerator` (implements `ISqlGenerator`), which lets you generate the SQL code based on your `EdmModel`.\n\nWhen you want to let the Entity Framework create database if it does not exist, just set `SqliteDropCreateDatabaseAlways\u003c\u003e` or `SqliteCreateDatabaseIfNotExists\u003c\u003e` as your `IDbInitializer\u003c\u003e`.\n\n### Initializer Sample\n\n```csharp\npublic class MyDbContext : DbContext\n{\n    public MyDbContext()\n        : base(\"ConnectionStringName\") { }\n\n    protected override void OnModelCreating(DbModelBuilder modelBuilder)\n    {\n        var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists\u003cMyDbContext\u003e(modelBuilder);\n        Database.SetInitializer(sqliteConnectionInitializer);\n    }\n}\n```\n\nNotice that the `SqliteDropCreateDatabaseWhenModelChanges\u003c\u003e` initializer will create a additional table in your database.\nThis table is used to store some information to detect model changes. If you want to use an own entity/table you have to implement the\n`IHistory` interface and pass the type of your entity as parameter to the constructor of the initializer.\n\nIn a more advanced scenario, you may want to populate some core- or test-data after the database was created.\nTo do this, inherit from `SqliteDropCreateDatabaseAlways\u003c\u003e`, `SqliteCreateDatabaseIfNotExists\u003c\u003e` or `SqliteDropCreateDatabaseWhenModelChanges\u003c\u003e` and override the `Seed(MyDbContext context)` function.\nThis function will be called in a transaction, once the database was created.  This function is only executed if a new database was successfully created.\n\n```csharp\npublic class MyDbContextInitializer : SqliteDropCreateDatabaseAlways\u003cMyDbContext\u003e\n{\n    public MyDbContextInitializer(DbModelBuilder modelBuilder)\n        : base(modelBuilder) { }\n\n    protected override void Seed(MyDbContext context)\n    {\n        context.Set\u003cPlayer\u003e().Add(new Player());\n    }\n}\n```\n\n### SqliteDatabaseCreator Sample\n\n```csharp\npublic class MyContext : DbContext\n{\n    protected override void OnModelCreating(DbModelBuilder modelBuilder)\n    {\n        var model = modelBuilder.Build(Database.Connection);\n        IDatabaseCreator sqliteDatabaseCreator = new SqliteDatabaseCreator();\n        sqliteDatabaseCreator.Create(Database, model);\n    }\n}\n```\n\n### SqliteSqlGenerator Sample\n\n```csharp\npublic class MyContext : DbContext\n{\n    protected override void OnModelCreating(DbModelBuilder modelBuilder)\n    {\n        var model = modelBuilder.Build(Database.Connection);\n        ISqlGenerator sqlGenerator = new SqliteSqlGenerator();\n        string sql = sqlGenerator.Generate(model.StoreModel);\n    }\n}\n```\n\n### .NET Core example\n\nAdd the following package references.\n```xml\n\u003cPackageReference Include=\"System.Data.SQLite\" Version=\"1.0.112.2\" /\u003e\n\u003cPackageReference Include=\"System.Data.SQLite.EF6\" Version=\"1.0.112.2\" /\u003e\n```\n\nAdd the following class.\n```csharp\npublic class MyConfiguration : DbConfiguration, IDbConnectionFactory {\n    public MyConfiguration()\n    {\n        SetProviderFactory(\"System.Data.SQLite\", SQLiteFactory.Instance);\n        SetProviderFactory(\"System.Data.SQLite.EF6\", SQLiteProviderFactory.Instance);\n\n        var providerServices = (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices));\n\n        SetProviderServices(\"System.Data.SQLite\", providerServices);\n        SetProviderServices(\"System.Data.SQLite.EF6\", providerServices);\n\n        SetDefaultConnectionFactory(this);\n    }\n\n    public DbConnection CreateConnection(string connectionString)\n        =\u003e new SQLiteConnection(connectionString);\n    }\n}\n```\n\nAlso, make sure you specify the DbConfigurationType on the DBContext class as well\n\n```csharp\n[DbConfigurationType(typeof(MyConfiguration))]\npublic class Context: DbContext {\n    //... DBContext things\n}\n```\n## Structure\n\nThe code is written in an extensible way.\nThe logic is divided into two main parts, Builder and Statement.\nThe Builder knows how to translate the EdmModel into statements where a statement class creates the SQLite-DDL-Code.\nThe structure of the statements is influenced by the [SQLite Language Specification](https://www.sqlite.org/lang.html).\nYou will find an extensive usage of the composite pattern.\n\n## Hints\n\nIf you try to reinstall the NuGet-Packages (e.g. if you want to downgrade to .NET 4.0), the app.config will be overwritten and you may getting an exception when you try to run the console project.\nIn this case please check the following issue: \u003chttps://github.com/msallin/SQLiteCodeFirst/issues/13.\u003e\n\n## Recognition\n\nI started with the [code](https://gist.github.com/flaub/1968486e1b3f2b9fddaf) from [flaub](https://github.com/flaub).\n","funding_links":["https://github.com/sponsors/msallin","https://www.paypal.com/cgi-bin/webscr?cmd=_donations\u0026business=ARTMHALNW4VC6\u0026lc=CH\u0026item_name=SQLite%2eCodeFirst\u0026item_number=sqlitecodefirst\u0026currency_code=CHF\u0026bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmsallin%2Fsqlitecodefirst","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmsallin%2Fsqlitecodefirst","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmsallin%2Fsqlitecodefirst/lists"}