Skip to content

Commit 38d51a8

Browse files
2881028810
authored andcommitted
- 增加 TableAttribute 特性属性 DisableSyncStructure,当实体对应的是视图时,可使用本功能禁用迁移 #61
- 增加 FreeSqlBuilder UseEntityPropertyNameConvert() 全局转换实体属性名方法 #60
1 parent ab658ca commit 38d51a8

File tree

18 files changed

+180
-79
lines changed

18 files changed

+180
-79
lines changed

Extensions/FreeSql.Extensions.LazyLoading/FreeSql.Extensions.LazyLoading.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
5-
<Version>0.6.9</Version>
5+
<Version>0.6.10</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>YeXiangQin</Authors>
88
<Description>FreeSql 扩展包,可实现【延时加载】属性.</Description>

FreeSql.Tests/DataAnnotations/MySqlFluentTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FreeSql.DataAnnotations;
2+
using MySql.Data.MySqlClient;
23
using System;
34
using System.Linq;
45
using Xunit;
@@ -9,6 +10,22 @@ public class MySqlFluentTest {
910
public MySqlFluentTest() {
1011
}
1112

13+
[Fact]
14+
public void DisableSyncStructure() {
15+
Assert.Throws<MySqlException>(() => g.mysql.Select<ModelDisableSyncStructure>().ToList());
16+
17+
g.mysql.Select<ModelSyncStructure>().ToList();
18+
}
19+
[Table(DisableSyncStructure = true)]
20+
class ModelDisableSyncStructure {
21+
[Column(IsPrimary = false)]
22+
public int pkid { get; set; }
23+
}
24+
class ModelSyncStructure {
25+
[Column(IsPrimary = false)]
26+
public int pkid { get; set; }
27+
}
28+
1229
[Fact]
1330
public void AopConfigEntity() {
1431
g.mysql.CodeFirst.ConfigEntity<ModelAopConfigEntity>(a => a.Property(b => b.pkid).IsPrimary(true));

FreeSql.Tests/DataAnnotations/SqlServerFluentTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using FreeSql.DataAnnotations;
22
using FreeSql.Tests.DataContext.SqlServer;
33
using System;
4+
using System.Data.SqlClient;
45
using Xunit;
56

67
namespace FreeSql.Tests.DataAnnotations {
@@ -14,6 +15,22 @@ public SqlServerFluentTest(SqlServerFixture sqlserverFixture)
1415
_sqlserverFixture = sqlserverFixture;
1516
}
1617

18+
[Fact]
19+
public void DisableSyncStructure() {
20+
Assert.Throws<SqlException>(() => _sqlserverFixture.SqlServer.Select<ModelDisableSyncStructure>().ToList());
21+
22+
_sqlserverFixture.SqlServer.Select<ModelSyncStructure>().ToList();
23+
}
24+
[Table(DisableSyncStructure = true)]
25+
class ModelDisableSyncStructure {
26+
[Column(IsPrimary = false)]
27+
public int pkid { get; set; }
28+
}
29+
class ModelSyncStructure {
30+
[Column(IsPrimary = false)]
31+
public int pkid { get; set; }
32+
}
33+
1734
[Fact]
1835
public void Fluent() {
1936
_sqlserverFixture.SqlServer.CodeFirst

FreeSql/DataAnnotations/TableAttribute.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public class TableAttribute : Attribute {
1717
/// </summary>
1818
public string SelectFilter { get; set; }
1919

20+
internal bool? _DisableSyncStructure;
21+
/// <summary>
22+
/// 禁用 CodeFirst 同步结构迁移
23+
/// </summary>
24+
public bool DisableSyncStructure { get => _DisableSyncStructure ?? false; set => _DisableSyncStructure = value; }
25+
2026
internal ConcurrentDictionary<string, ColumnAttribute> _columns { get; } = new ConcurrentDictionary<string, ColumnAttribute>(StringComparer.CurrentCultureIgnoreCase);
2127
}
2228
}

FreeSql/DataAnnotations/TableFluent.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public TableFluent SelectFilter(string value) {
3838
return this;
3939
}
4040

41+
/// <summary>
42+
/// 禁用 CodeFirst 同步结构迁移
43+
/// </summary>
44+
public TableFluent DisableSyncStructure(bool value) {
45+
_table.DisableSyncStructure = value;
46+
return this;
47+
}
48+
4149
public ColumnFluent Property(string proto) {
4250
if (_properties.ContainsKey(proto) == false) throw new KeyNotFoundException($"找不到属性名 {proto}");
4351
var col = _table._columns.GetOrAdd(proto, name => new ColumnAttribute { Name = proto });
@@ -74,6 +82,14 @@ public TableFluent<T> SelectFilter(string value) {
7482
return this;
7583
}
7684

85+
/// <summary>
86+
/// 禁用 CodeFirst 同步结构迁移
87+
/// </summary>
88+
public TableFluent<T> DisableSyncStructure(bool value) {
89+
_table.DisableSyncStructure = value;
90+
return this;
91+
}
92+
7793
public ColumnFluent Property<TProto>(Expression<Func<T, TProto>> column) {
7894
var proto = (column.Body as MemberExpression)?.Member;
7995
if (proto == null) throw new FormatException($"错误的表达式格式 {column}");

FreeSql/FreeSql.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
5-
<Version>0.6.9</Version>
5+
<Version>0.6.10</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>YeXiangQin</Authors>
88
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>

FreeSql/FreeSql.xml

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/FreeSqlBuilder.cs

Lines changed: 34 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public IFreeSql<TMark> Build<TMark>() {
147147
ret = Activator.CreateInstance(type, new object[] { _masterConnectionString, _slaveConnectionString }) as IFreeSql<TMark>;
148148
if (ret != null) {
149149
ret.CodeFirst.IsAutoSyncStructure = _isAutoSyncStructure;
150-
150+
151151
ret.CodeFirst.IsSyncStructureToLower = _isSyncStructureToLower;
152152
ret.CodeFirst.IsSyncStructureToUpper = _isSyncStructureToUpper;
153153
ret.CodeFirst.IsConfigEntityFromDbFirst = _isConfigEntityFromDbFirst;
@@ -157,73 +157,39 @@ public IFreeSql<TMark> Build<TMark>() {
157157
ado.AopCommandExecuting += _aopCommandExecuting;
158158
ado.AopCommandExecuted += _aopCommandExecuted;
159159

160-
//添加实体属性名全局AOP转换处理
161-
if (_entityPropertyConvertType != StringConvertType.None)
162-
{
163-
// 局部方法判断是否存在Column特性以及是否设置Name的值
164-
bool CheckEntityPropertyColumnAttribute(PropertyInfo propertyInfo)
165-
{
166-
var attr = propertyInfo.GetCustomAttribute<ColumnAttribute>();
167-
if (attr == null || string.IsNullOrEmpty(attr.Name))
168-
{
169-
return true;
170-
}
171-
172-
return false;
173-
}
174-
175-
switch (_entityPropertyConvertType)
176-
{
177-
case StringConvertType.Lower:
178-
ret.Aop.ConfigEntityProperty = (s, e) =>
179-
{
180-
if (CheckEntityPropertyColumnAttribute(e.Property))
181-
{
182-
e.ModifyResult.Name = e.Property.Name.ToLower();
183-
}
184-
};
185-
break;
186-
case StringConvertType.Upper:
187-
ret.Aop.ConfigEntityProperty = (s, e) =>
188-
{
189-
if (CheckEntityPropertyColumnAttribute(e.Property))
190-
{
191-
e.ModifyResult.Name = e.Property.Name.ToUpper();
192-
}
193-
};
194-
break;
195-
case StringConvertType.PascalCaseToUnderscore:
196-
ret.Aop.ConfigEntityProperty = (s, e) =>
197-
{
198-
if (CheckEntityPropertyColumnAttribute(e.Property))
199-
{
200-
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name);
201-
}
202-
};
203-
break;
204-
case StringConvertType.PascalCaseToUnderscoreWithLower:
205-
ret.Aop.ConfigEntityProperty = (s, e) =>
206-
{
207-
if (CheckEntityPropertyColumnAttribute(e.Property))
208-
{
209-
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name).ToLower();
210-
}
211-
};
212-
break;
213-
case StringConvertType.PascalCaseToUnderscoreWithUpper:
214-
ret.Aop.ConfigEntityProperty = (s, e) =>
215-
{
216-
if (CheckEntityPropertyColumnAttribute(e.Property))
217-
{
218-
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name).ToUpper();
219-
}
220-
};
221-
break;
222-
default:
223-
break;
224-
}
225-
}
226-
}
160+
//添加实体属性名全局AOP转换处理
161+
if (_entityPropertyConvertType != StringConvertType.None) {
162+
switch (_entityPropertyConvertType) {
163+
case StringConvertType.Lower:
164+
ret.Aop.ConfigEntityProperty = (s, e) => {
165+
e.ModifyResult.Name = e.Property.Name.ToLower();
166+
};
167+
break;
168+
case StringConvertType.Upper:
169+
ret.Aop.ConfigEntityProperty = (s, e) => {
170+
e.ModifyResult.Name = e.Property.Name.ToUpper();
171+
};
172+
break;
173+
case StringConvertType.PascalCaseToUnderscore:
174+
ret.Aop.ConfigEntityProperty = (s, e) => {
175+
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name);
176+
};
177+
break;
178+
case StringConvertType.PascalCaseToUnderscoreWithLower:
179+
ret.Aop.ConfigEntityProperty = (s, e) => {
180+
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name).ToLower();
181+
};
182+
break;
183+
case StringConvertType.PascalCaseToUnderscoreWithUpper:
184+
ret.Aop.ConfigEntityProperty = (s, e) => {
185+
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name).ToUpper();
186+
};
187+
break;
188+
default:
189+
break;
190+
}
191+
}
192+
}
227193

228194
return ret;
229195
}

FreeSql/Internal/CommonProvider/CodeFirstProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public CodeFirstProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression
4545
internal ConcurrentDictionary<string, bool> dicSyced = new ConcurrentDictionary<string, bool>();
4646
public bool SyncStructure<TEntity>() => this.SyncStructure(typeof(TEntity));
4747
public bool SyncStructure(params Type[] entityTypes) {
48-
if (entityTypes == null) return true;
49-
var syncTypes = entityTypes.Where(a => dicSyced.ContainsKey(a.FullName) == false).ToArray();
50-
if (syncTypes.Any() == false) return true;
48+
if (entityTypes == null) return false;
49+
var syncTypes = entityTypes.Where(a => dicSyced.ContainsKey(a.FullName) == false && GetTableByEntity(a)?.DisableSyncStructure == false).ToArray();
50+
if (syncTypes.Any() == false) return false;
5151
var before = new Aop.SyncStructureBeforeEventArgs(entityTypes);
5252
_orm.Aop.SyncStructureBefore?.Invoke(this, before);
5353
Exception exception = null;

FreeSql/Internal/CommonUtils.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public TableAttribute GetEntityTableAttribute(Type type) {
7373
if (!string.IsNullOrEmpty(trytb.Name)) attr.Name = trytb.Name;
7474
if (!string.IsNullOrEmpty(trytb.OldName)) attr.OldName = trytb.OldName;
7575
if (!string.IsNullOrEmpty(trytb.SelectFilter)) attr.SelectFilter = trytb.SelectFilter;
76+
if (trytb._DisableSyncStructure != null) attr._DisableSyncStructure = trytb.DisableSyncStructure;
77+
7678
}
7779
var attrs = type.GetCustomAttributes(typeof(TableAttribute), false);
7880
foreach (var tryattrobj in attrs) {
@@ -81,10 +83,12 @@ public TableAttribute GetEntityTableAttribute(Type type) {
8183
if (!string.IsNullOrEmpty(tryattr.Name)) attr.Name = tryattr.Name;
8284
if (!string.IsNullOrEmpty(tryattr.OldName)) attr.OldName = tryattr.OldName;
8385
if (!string.IsNullOrEmpty(tryattr.SelectFilter)) attr.SelectFilter = tryattr.SelectFilter;
86+
if (tryattr._DisableSyncStructure != null) attr._DisableSyncStructure = tryattr.DisableSyncStructure;
8487
}
8588
if (!string.IsNullOrEmpty(attr.Name)) return attr;
8689
if (!string.IsNullOrEmpty(attr.OldName)) return attr;
8790
if (!string.IsNullOrEmpty(attr.SelectFilter)) return attr;
91+
if (attr._DisableSyncStructure != null) return attr;
8892
return null;
8993
}
9094
public ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto) {

0 commit comments

Comments
 (0)