-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.cs
161 lines (128 loc) · 4.09 KB
/
Database.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using Microsoft.EntityFrameworkCore;
using System;
namespace TRPO_3
{
public enum TableType
{
Client,
Attorney,
Case,
Archive,
Article
}
public class AttorneyContext : DbContext
{
public DbSet<Human> Human { get; set; }
public DbSet<Attorney> Attorney { get; set; }
public DbSet<Client> Client { get; set; }
public DbSet<Case> Case { get; set; }
public DbSet<Article> Article { get; set; }
public string DbPath { get; }
public AttorneyContext()
{
var folder = Environment.SpecialFolder.MyDocuments;
var path = Environment.GetFolderPath(folder);
DbPath = System.IO.Path.Join(path, "database.db");
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite($"Data Source={DbPath}")
.LogTo(s => System.Diagnostics.Debug.WriteLine(s));
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Human>()
.Property(b => b.HumanId)
.IsRequired();
modelBuilder.Entity<Human>()
.Ignore(x => x.FullName);
modelBuilder.Entity<Attorney>()
.Property(b => b.AttorneyId)
.IsRequired();
modelBuilder.Entity<Client>()
.Property(b => b.ClientId)
.IsRequired();
modelBuilder.Entity<Case>()
.Property(b => b.CaseId)
.IsRequired();
modelBuilder.Entity<Case>()
.Property(b => b.AttorneyId)
.IsRequired();
modelBuilder.Entity<Case>()
.Property(b => b.ClientId)
.IsRequired();
modelBuilder.Entity<Case>()
.Property(b => b.ArticleId)
.IsRequired();
modelBuilder.Entity<Case>()
.Property(x => x.Archived)
.HasDefaultValue(false);
modelBuilder.Entity<Case>()
.Property(x => x.Pay)
.HasDefaultValue(0);
modelBuilder.Entity<Article>()
.Property(b => b.ArticleId)
.IsRequired();
}
}
public class Human
{
public int HumanId { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Patronymic { get; set; }
public virtual string FullName => $"{Name} {Patronymic} {Surname}";
}
public class Attorney
{
public int AttorneyId { get; set; }
public Attorney()
{
}
public Attorney(Human human)
{
Human = human;
HumanId = human.HumanId;
}
public int HumanId { get; set; }
public virtual Human Human { get; set; }
}
public class Client
{
public int ClientId { get; set; }
public Client()
{
}
public Client(Human human)
{
Human = human;
HumanId = human.HumanId;
}
public int HumanId { get; set; }
public virtual Human Human { get; set; }
}
public class Article
{
public int ArticleId { get; set; }
public string Name { get; set; }
public int ExprectedPunishment { get; set; }
}
public class Case
{
public int CaseId { get; set; }
public int AttorneyId { get; set; }
public int ClientId { get; set; }
public int ArticleId { get; set; }
public int? FinalPunishment { get; set; }
public Case()
{
}
public virtual Attorney Attorney { get; set; }
public virtual Article Article { get; set; }
public virtual Client Client { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public int Pay { get; set; }
public string Comment { get; set; }
public bool Archived { get; set; }
}
}