-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathRepositoryTests02.cs
153 lines (113 loc) · 4.26 KB
/
RepositoryTests02.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
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace FreeSql.Tests.DbContext
{
public class RepositoryTests02
{
[Fact]
public void TestMethod1()
{
using (IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=:memory:")
.UseMonitorCommand(cmd => Trace.WriteLine($"Sql:{cmd.CommandText}"))//监听SQL语句
.UseAutoSyncStructure(true) //自动同步实体结构到数据库,FreeSql不会扫描程序集,只有CRUD时才会生成表。
.Build())
{
fsql.GlobalFilter.ApplyIf<User>("TenantFilter", () => TenantManager.Current > 0, a => a.TenantId == TenantManager.Current);
fsql.Aop.AuditValue += (_, e) =>
{
if (TenantManager.Current > 0 && e.Property.PropertyType == typeof(int) && e.Property.Name == "TenantId")
{
e.Value = TenantManager.Current;
};
};
IBaseRepository<User> resp = fsql.GetRepository<User>();
resp.Delete(a => a.ID != null);
Assert.True(resp != null);
TenantManager.Current = 1;
resp.InsertOrUpdate(new User()
{
uname = "zhaoqin",
});
resp.InsertOrUpdate(new User()
{
uname = "wanghuan",
});
long cc = resp.Where(a => a.ID != null).Count();
Assert.True(cc == 2);
TenantManager.Current = 2;
resp.InsertOrUpdate(new User()
{
uname = "zhaoqin1",
});
resp.InsertOrUpdate(new User()
{
uname = "wanghuan1",
});
long c = resp.Where(a => a.ID != null).Count();
Assert.True(c == 2);
TenantManager.Current = 0;
Assert.True(resp.Where(a => a.ID != null).Count() == 4);
//多租户启用,但表达式想取消,这个可以成功
TenantManager.Current = 2;
long count1 = fsql.Select<User>().DisableGlobalFilter().Count();
Assert.True(count1 == 4);
Console.WriteLine("仓储的过滤器禁止,但不成功.");
//仓储的过滤器禁止,但不成功.
//using (resp.DataFilter.DisableAll())
//{
// long count2 = resp.Where(a => a.ID != null).Count();
// Assert.True(count2 == 4);
//}
}
}
public class TenantManager
{
// 注意一定是 static 静态化
static AsyncLocal<int> _asyncLocal = new AsyncLocal<int>();
public static int Current
{
get => _asyncLocal.Value;
set => _asyncLocal.Value = value;
}
}
public class BaseModel
{
[Column(IsIdentity = true)]
public int? ID { get; set; }
public int TenantId { get; set; }
}
public class User : BaseModel
{
public Guid cateId { get; set; }
public Cate cate { get; set; }
public string uname { get; set; }
public int age { get; set; }
public List<Group> groups { get; set; } = new List<Group>();
}
public class Cate : BaseModel
{
public string catename { get; set; }
public List<User> users { get; set; }
}
public class Group : BaseModel
{
public string groupname { get; set; }
public List<User> users { get; set; } = new List<User>();
}
public class User_Group
{
public Guid UserId { get; set; }
public User user { get; set; }
public Guid GroupId { get; set; }
public Group group { get; set; }
}
}
}