简体   繁体   English

带有 Entity Framework Core 的 SQLite 很慢

[英]SQLite with Entity Framework Core is slow

Inserting a record to SQLite DB via Entity Framework Core in C# in .NET Core is extremely slow.在 .NET Core 中通过 C# 中的 Entity Framework Core 向 SQLite DB 插入记录非常慢。 It is 10 times slower than my expectation.它比我的预期慢 10 倍。 Is there a magic to improve performance?有没有提高性能的魔法?

public class MyRecord
{
    [Key]
    public int ID { get; set; }
    public int Value { get; set; }
}

public class MyDatabase : DbContext
{
    public DbSet<MyRecord> Records { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Data source=mydb.db");
#if DEBUG
        optionsBuilder.EnableSensitiveDataLogging(true);
#endif
    }
}

public class MyDatabaseTests
{
    public MyDatabaseTests()
    {
        var db = new MyDatabase();
        db.Database.EnsureDeleted();
        db.Database.EnsureCreated();
    }

    [TestMethod]
    public void EntityFramework1000Transactions()
    {
        for (int i = 0; i < 1000; i++)
        {
            using var db = new MyDatabase();
            var rec = new MyRecord
            {
                ID = i+1,
                Value = 123456789
            };
            var tx = db.Database.BeginTransaction();
            db.Records.Add(rec);
            db.SaveChanges();
            tx.Commit();
        }
    }
}

EntityFramework1000Transactions takes 10 seconds on my PC with SATA-SSD drive. EntityFramework1000Transactions在我的带有 SATA-SSD 驱动器的 PC 上需要 10 秒。 Replacing to eNVM is not my option because I am writing an application for a sort of embedded system which has equivalent storage.替换为 eNVM 不是我的选择,因为我正在为一种具有等效存储的嵌入式系统编写应用程序。

I tried using db.ChangeTracker.AutoDetectChangesEnabled = false;我尝试使用db.ChangeTracker.AutoDetectChangesEnabled = false; , or db.BulkSaveChanges(); , 或db.BulkSaveChanges(); (with Z.EntityFramework.Extensions.EFCore) (使用 Z.EntityFramework.Extensions.EFCore)

And they didn't help me.他们没有帮助我。

My environment:我的环境:

  • Visual Studio 2019 Version 16.11.2 Visual Studio 2019 版本 16.11.2
  • .NET Core 3.1 .NET 核心 3.1
  • C# C#
  • Microsoft.EntityFrameworkCore.Sqlite 5.0.11 Microsoft.EntityFrameworkCore.Sqlite 5.0.11
  • Windows 10视窗 10

Edit编辑

The loop was a simulation of a process in the application.该循环是对应用程序中的一个过程的模拟。 They are not single transaction.它们不是单笔交易。 The loop is intended to simulate 1000 transactions.该循环旨在模拟 1000 个事务。 Therefore they cannot be in the same transaction.因此它们不能在同一个事务中。

It also cannot remove transaction.它也不能删除事务。 The transactions in the application is actually more complicate and can update two or more tables/records at the same time.应用程序中的事务实际上更复杂,可以同时更新两个或多个表/记录。 It must be handled in a transaction.它必须在事务中处理。 therefore removing transaction is not my option.因此删除交易不是我的选择。

将连接命中数(在 for 循环内)限制到数据库可以解决线程中提到的问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM