[BUG] Deleting a non existing collection causes insert operation to fail
Version LiteDb 5.0.8.0 .net RunTime v4.0.30319 VS 2019 Windows Forms Application
Describe the bug
Deleting a non existing collection causes insert operation to fail. InsertBulk returns successful result but data do not get inserted to the database.
public class AddressOfflineReporsitory : IAddressOfflineReporsitory
{
public async Task<int> InsertAddresses(IList<Address> addresses)
{
var Result = 0;
await Task<int>.Run(() =>
{
using (var db = LiteDBClient.CreateCon(LiteDBClient.DBType.CustomerAccount))
{
db.BeginTrans();
var col = db.GetCollection<Address>();
col.EnsureIndex(a => a.Name);
col.EnsureIndex(a => a.No);
col.EnsureIndex(a => a.Contact);
Result = col.InsertBulk(addresses);
db.Commit();
}
});
return Result;
}
public async Task DeleteAllAddresses()
{
await Task.Run(() =>
{
using (var db = LiteDBClient.CreateCon(LiteDBClient.DBType.CustomerAccount))
{
var col = db.GetCollection<Address>();
col.DeleteAll();
}
});
}
}
Usage
await addressOfflineReporsitory.DeleteAllAddresses();
var savedcount = await addressOfflineReporsitory.InsertAddresses(ServiceResult.Data.ToList());
logger.Information("{AddressBookRecordCount} Address entry downloaded and saved.", savedcount);
Expected behavior Data to be inserted. Insertbulk method returns correct number although data is not inserted.
Additional context It works as expected when you change delete method as :
if (!db.CollectionExists(nameof(Address))) return;
var col = db.GetCollection<Address>();
col.DeleteAll();
@alierdogan70 I could not reproduce the issue running LiteDB v5.0.8 on .NET Core v3.1.4.
Also, if I understood you correctly, you claim to be using .NET Framework 4.0, which I believe is not possible for two reasons:
- LiteDB v5.0.x dropped support for .NET Framework 4.0;
- You use async/await, which was only introduced in C# 5, and .NET Framework 4.0 only supported C# 4.
Hi @lbnascimento
It is the runtime (CLR) version:

Target Framework is : 4.7.2
Sorry for the confusion.
It is not very easy to reproduce the issue. Insert operation needs to be runned immediately after the delete operation as in the example on a brand new database. When I stop the flow during debugging I see database-log file being generated but it disappears after the insert operation.
@alierdogan70 I still haven't been able to reproduce the issue. I'm running on .NET Framework 4.7.2 and I tried running both in debug and in release mode.
The log file is meant to disappear when the LiteDatabase is disposed.
Try calling db.Checkpoint() right after db.Commit().
Hi @lbnascimento ,
I created a small application containing the bug. Please delete the database file before every try. If you uncomment AddressRepository.cs line 95 it works properly.

I hope this helps
@alierdogan70 This seems to be related to #1762, and your app finally allowed me to reproduce it and to fix it, so thanks for that.
Just to make sure, could you test with the latest master?
@lbnascimento I am having the same problem although I tried with latest master. .net 5-rc1, vs 2019 preview.
using var db = new LiteDatabase(new ConnectionString
{
Filename = "C:\\testing\\taskdb.ld",
Password = "xxx",
Connection = ConnectionType.Shared,
ReadOnly = false
});
var collection = db.GetCollection<BasicGuidValue>("task_guid");
Guid taskMainGuid = Guid.Parse("ac2b28f7-bce7-441c-adbb-7f3e62c89c5c");
var guidBasic = new BasicGuidValue(taskMainGuid);
collection.DeleteAll();
collection.Insert(guidBasic);
// BasicGuidValue obj ->
public class BasicGuidValue
{
[JsonConstructor]
[BsonCtor]
public BasicGuidValue(Guid value)
{
Value = value;
}
public Guid Value { get; set; }
}
After the code runs, the collection is not created in the database (If not exists). When I remove DeleteAll code line, perfectly created the collection in the database.
--
Same problem, different delete strategy;
using var db = new LiteDatabase(new ConnectionString
{
Filename = "C:\\testing\\taskdb.ld",
Password = "xxx",
Connection = ConnectionType.Shared,
ReadOnly = false
});
db.DropCollection("task_guid");
var collection = db.GetCollection<BasicGuidValue>("task_guid");
Guid taskMainGuid = Guid.Parse("ac2b28f7-bce7-441c-adbb-7f3e62c89c5c");
var guidBasic = new BasicGuidValue(taskMainGuid);
collection.Insert(guidBasic);
Still not creating the collection.
@alierdogan70 I could not reproduce the issue on .NET Core 3.1. I'm not currently able to test on .NET 5 (I may be in a few days). Is there any way you could test it with .NET Core 3.1?
@alierdogan70 I could not reproduce the issue on .NET Core 3.1. I'm not currently able to test on .NET 5 (I may be in a few days). Is there any way you could test it with .NET Core 3.1?
Hi. I am out of town for 3 weeks. I am going to try when I return back.
This is fixed on the dev branch. dev ships as the LiteDB 6.x prereleases.
Please try the latest prerelease (currently 6.0.0-prerelease.81):
dotnet add package LiteDB --prerelease
If the problem still happens on the prerelease, comment here with the version you used and a repro, and we'll reopen the issue.