-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
74 lines (59 loc) · 1.97 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace EfCoreNotNullNestedOwned
{
class Program
{
static void Main(string[] args)
{
using (var db = new ExampleContext())
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
for (int i = 0; i < 5; i++)
db.Add(new RootEntity());
db.SaveChanges();
}
// Comment this whole task and application will run without any errors.
Task.Run(() =>
{
using (var db1 = new ExampleContext())
{
db1.RootEntities.First().Child = new ChildEntity();
db1.SaveChanges();
}
});
using (var db2 = new ExampleContext())
{
var rootEntities = db2.RootEntities.OrderBy(i => i.Id);
foreach (var rootEntity in rootEntities)
{
// Wait a bit until db1.SaveChanges() above will finish execution.
Thread.Sleep(2000);
rootEntity.Child = new ChildEntity();
// Here will be an error Error: SQLite Error 5: 'database is locked'
db2.SaveChanges();
}
}
}
}
public class ExampleContext : DbContext
{
public DbSet<RootEntity> RootEntities { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlite($"Data Source=blogging.db");
}
public class RootEntity
{
public Int32 Id { get; set; }
public ChildEntity? Child { get; set; }
}
public class ChildEntity
{
public Int32 Id { get; set; }
public String? AProperty { get; set; }
}
}