8

I want to use SQlLite Database in an ASP.NET Core project but without using an Entity Framework.

I think I should use a class which derives from DbProviderFactory & which caters to SQLLite database (something like System.Data.SqlClient.SqlClientFactory.Instance which is used for sqlServer) but I am unable to find it.

All the examples which i came across suggest me to use entity framework, but I don't want to use it.

1 Answer 1

8

You have it in official repo: https://github.com/aspnet/Microsoft.Data.Sqlite

And here is an example: http://www.bricelam.net/2015/04/29/sqlite-on-corefx.html

EDIT: adding link content in case it disappears in the future.

The provider is built on top of the System.Data.Common contract. This contract is a very small subset of the ADO.NET provider model. Using the provider should feel very natural to anyone familiar with ADO.NET.

using (var connection = new SqliteConnection("" +
    new SqliteConnectionStringBuilder
    {
        DataSource = "hello.db"
    }))
{
    connection.Open();

    using (var transaction = connection.BeginTransaction())
    {
        var insertCommand = connection.CreateCommand();
        insertCommand.Transaction = transaction;
        insertCommand.CommandText = "INSERT INTO message ( text ) VALUES ( $text )";
        insertCommand.Parameters.AddWithValue("$text", "Hello, World!");
        insertCommand.ExecuteNonQuery();

        var selectCommand = connection.CreateCommand();
        selectCommand.Transaction = transaction;
        selectCommand.CommandText = "SELECT text FROM message";
        using (var reader = selectCommand.ExecuteReader())
        {
            while (reader.Read())
            {
                var message = reader.GetString(0);
                Console.WriteLine(message);
            }
        }

        transaction.Commit();
    }
}

Batching

The only real feature that the library adds to the native SQLite interfaces is batching. The native interfaces only support compiling and executing one statement at a time. This library implements batching in a way that should feel completely transparent. Here is an example of using batching.

using (var connection = new SqliteConnection("Data Source=hello.db"))
{
    var command = connection.CreateCommand();
    command.CommandText =
        "UPDATE message SET text = $text1 WHERE id = 1;" +
        "UPDATE message SET text = $text2 WHERE id = 2";
    command.Parameters.AddWithValue("$text1", "Hello");
    command.Parameters.AddWithValue("$text2", "World");

    connection.Open();
    command.ExecuteNonQuery();
}

Platforms:

Currently, Microsoft.Data.Sqlite works on the following platforms.

  • .NET Framework
  • Mono
  • .NET Core
  • .NET Native
  • CoreCLR
  • Windows Universal
4
  • @AbdulRehmanSayed The package you are looking for is "MySql.Data.Core" Commented Nov 17, 2016 at 11:20
  • Note that now, in the link above to Github for Microsoft.Data.Sqlite, the package is marked as Archived and as moved into EntityFrameworkCore, but it still works. You can also add the Microsoft.EntityFrameworkCore.Sqlite NuGet package and get Microsoft.Data.Sqlite.Core included as a dependency.
    – Bryan B
    Commented Apr 2, 2020 at 19:31
  • With microsoft.data.sqlite, is it possible to use linq instead if raw sql? (And without ef)
    – Hamid Z
    Commented Jul 25, 2021 at 9:55
  • @HamidZ, No you can't, but you could use Dapper as ORM Commented Jul 26, 2021 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.