简体   繁体   English

在ASP.NET核心项目中使用没有实体框架的SQLLite数据库

[英]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. 我想在ASP.NET Core项目中使用SQlLite数据库,但不使用实体框架。

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. 我想我应该使用一个派生自DbProviderFactory的类,它适合SQLLite数据库(类似于用于sqlServer的System.Data.SqlClient.SqlClientFactory.Instance ),但我无法找到它。

All the examples which i came across suggest me to use entity framework, but I don't want to use it. 我遇到的所有示例都建议我使用实体框架,但我不想使用它。

You have it in official repo: https://github.com/aspnet/Microsoft.Data.Sqlite 你有正式的回购: https//github.com/aspnet/Microsoft.Data.Sqlite

And here is an example: http://www.bricelam.net/2015/04/29/sqlite-on-corefx.html 这是一个例子: 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. 提供程序构建在System.Data.Common合同之上。 This contract is a very small subset of the ADO.NET provider model. 此合约是ADO.NET提供程序模型的一个非常小的子集。 Using the provider should feel very natural to anyone familiar with ADO.NET. 对于熟悉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. 库添加到本机SQLite接口的唯一真正功能是批处理。 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. 目前,Microsoft.Data.Sqlite适用于以下平台。

  • .NET Framework .NET Framework
  • Mono
  • .NET Core .NET核心
  • .NET Native .NET Native
  • CoreCLR CoreCLR
  • Windows Universal Windows Universal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在没有实体框架和迁移的ASP.NET Core MVC应用程序中使用ASP.NET标识 - Using ASP.NET Identity in an ASP.NET Core MVC application without Entity Framework and Migrations Entity Framework Core 和 ASP.NET Core MVC 中的 Inheritance 数据库 - Inheritance Database in Entity Framework Core and ASP.NET Core MVC 如何在 ubuntu 桌面上使用 Entity Framework Core 将 asp.net core MVC 项目连接到本地数据库 - how to connect asp.net core MVC project to local DB using Entity Framework Core on ubuntu desktop 如何使用Visual Studio代码将实体框架核心安装到asp.net核心3项目 - How to install entity framework core to asp.net core 3 project using visual studio code 如何在ASP.NET Core项目中设置实体框架 - How to setup Entity Framework in ASP.NET Core project 如何在没有实体框架的情况下连接到 ASP.NET Core 中的数据库? - How can I connect to a database in ASP.NET Core without Entity Framework? ASP.NET 核心 Web API 使用实体框架核心 - ASP.NET Core Web API using Entity Framework Core ASP.net Core Application Entity Framework 6无法生成数据库 - ASP.net Core Application Entity Framework 6 not generating database 没有asp.net应用程序的实体框架核心种子 - Entity Framework core seed without asp.net app 使用实体框架在ASP.Net Core 2中动态创建数据库表 - Dynamic database table creation in ASP.Net Core 2 with Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM