Abdul Rehman Sayed Abdul Rehman Sayed - 1 year ago 150
C# Question

using SQLLite Database without entity Framework in ASP.NET Core Project

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

& 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.

Answer Source

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"

    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!");

        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);



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");



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

  • .NET Framework
  • Mono
  • .NET Core
  • .NET Native
  • CoreCLR
  • Windows Universal
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download