Skip to content

Commit 163fe89

Browse files
2881028810
28810
authored and
28810
committed
- 补充 nuget 包增加 xmlDoc 编译;
- 调整 Column.Unique 定义规则,解决同一属性不可配置多次的问题;
1 parent d49be98 commit 163fe89

File tree

10 files changed

+50
-28
lines changed

10 files changed

+50
-28
lines changed

FreeSql.Tests/MySql/MySqlCodeFirstTest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class AddUniquesInfo {
4545
[Column(Unique = "uk_phone")]
4646
public string phone { get; set; }
4747

48-
[Column(Unique = "uk_group_index")]
48+
[Column(Unique = "uk_group_index, uk_group_index22")]
4949
public string group { get; set; }
50-
[Column(Unique = "uk_group_index11")]
50+
[Column(Unique = "uk_group_index")]
5151
public int index { get; set; }
52-
[Column(Unique = "uk_group_index222")]
52+
[Column(Unique = "uk_group_index22")]
5353
public string index22 { get; set; }
5454
}
5555

FreeSql.Tests/Oracle/OracleCodeFirstTest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class AddUniquesInfo {
4545
[Column(Unique = "uk_phone")]
4646
public string phone { get; set; }
4747

48-
[Column(Unique = "uk_group_index")]
48+
[Column(Unique = "uk_group_index, uk_group_index22")]
4949
public string group { get; set; }
50-
[Column(Unique = "uk_group_index11")]
50+
[Column(Unique = "uk_group_index")]
5151
public int index { get; set; }
52-
[Column(Unique = "uk_group_index222")]
52+
[Column(Unique = "uk_group_index22")]
5353
public string index22 { get; set; }
5454
}
5555
[Fact]

FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ class AddUniquesInfo {
5252
[Column(Unique = "uk_phone")]
5353
public string phone { get; set; }
5454

55-
[Column(Unique = "uk_group_index")]
55+
[Column(Unique = "uk_group_index, uk_group_index22")]
5656
public string group { get; set; }
57-
[Column(Unique = "uk_group_index11")]
57+
[Column(Unique = "uk_group_index")]
5858
public int index { get; set; }
59-
[Column(Unique = "uk_group_index222")]
59+
[Column(Unique = "uk_group_index22")]
6060
public string index22 { get; set; }
6161
}
6262

FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ class AddUniquesInfo {
5656
[Column(Unique = "uk_phone")]
5757
public string phone { get; set; }
5858

59-
[Column(Unique = "uk_group_index")]
59+
[Column(Unique = "uk_group_index, uk_group_index22")]
6060
public string group { get; set; }
61-
[Column(Unique = "uk_group_index11")]
61+
[Column(Unique = "uk_group_index")]
6262
public int index { get; set; }
63-
[Column(Unique = "uk_group_index222")]
63+
[Column(Unique = "uk_group_index22")]
6464
public string index22 { get; set; }
6565
}
6666

FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class AddUniquesInfo {
4545
[Column(Unique = "uk_phone")]
4646
public string phone { get; set; }
4747

48-
[Column(Unique = "uk_group_index")]
48+
[Column(Unique = "uk_group_index, uk_group_index22")]
4949
public string group { get; set; }
50-
[Column(Unique = "uk_group_index111")]
50+
[Column(Unique = "uk_group_index")]
5151
public int index { get; set; }
52-
[Column(Unique = "uk_group_index222")]
52+
[Column(Unique = "uk_group_index22")]
5353
public string index22 { get; set; }
5454
}
5555

FreeSql/DataAnnotations/ColumnAttribute.cs

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23

34
namespace FreeSql.DataAnnotations {
45
public class ColumnAttribute : Attribute {
@@ -38,10 +39,30 @@ public class ColumnAttribute : Attribute {
3839
/// </summary>
3940
public bool IsVersion { get => _IsVersion ?? false; set => _IsVersion = value; }
4041

42+
internal string[] _Uniques;
4143
/// <summary>
42-
/// 唯一键,多个属性指定相同的标识,代表联合键
44+
/// 唯一键,在多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。
4345
/// </summary>
44-
public string Unique { get; set; }
46+
public string Unique {
47+
get => _Uniques == null ? null : string.Join(", ", _Uniques);
48+
set {
49+
if (string.IsNullOrEmpty(value)) {
50+
_Uniques = null;
51+
return;
52+
}
53+
var val = value?.Trim(' ', '\t', ',');
54+
if (string.IsNullOrEmpty(val)) {
55+
_Uniques = null;
56+
return;
57+
}
58+
var arr = val.Split(',').Select(a => a.Trim(' ', '\t').Trim()).Where(a => !string.IsNullOrEmpty(a)).ToArray();
59+
if (arr.Any() == false) {
60+
_Uniques = null;
61+
return;
62+
}
63+
_Uniques = arr;
64+
}
65+
}
4566
/// <summary>
4667
/// 数据库默认值
4768
/// </summary>

FreeSql/DataAnnotations/ColumnFluent.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public ColumnFluent IsVersion(bool value) {
6666
return this;
6767
}
6868
/// <summary>
69-
/// 唯一键,多个属性指定相同的标识,代表联合键
69+
/// 唯一键,在多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。
7070
/// </summary>
7171
/// <param name="value">标识</param>
7272
/// <returns></returns>

FreeSql/FreeSql.xml

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/Internal/CommonUtils.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ internal ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto)
101101
if (trycol._IsNullable != null) attr._IsNullable = trycol.IsNullable;
102102
if (trycol._IsIgnore != null) attr._IsIgnore = trycol.IsIgnore;
103103
if (trycol._IsVersion != null) attr._IsVersion = trycol.IsVersion;
104-
if (!string.IsNullOrEmpty(trycol.Unique)) attr.Unique = trycol.Unique;
104+
if (trycol._Uniques != null) attr._Uniques = trycol._Uniques;
105105
if (trycol.MapType != null) attr.MapType = trycol.MapType;
106106
if (trycol.DbDefautValue != null) attr.DbDefautValue = trycol.DbDefautValue;
107107
}
@@ -117,7 +117,7 @@ internal ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto)
117117
if (tryattr._IsNullable != null) attr._IsNullable = tryattr.IsNullable;
118118
if (tryattr._IsIgnore != null) attr._IsIgnore = tryattr.IsIgnore;
119119
if (tryattr._IsVersion != null) attr._IsVersion = tryattr.IsVersion;
120-
if (!string.IsNullOrEmpty(tryattr.Unique)) attr.Unique = tryattr.Unique;
120+
if (tryattr._Uniques != null) attr._Uniques = tryattr._Uniques;
121121
if (tryattr.MapType != null) attr.MapType = tryattr.MapType;
122122
if (tryattr.DbDefautValue != null) attr.DbDefautValue = tryattr.DbDefautValue;
123123
}
@@ -130,7 +130,7 @@ internal ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto)
130130
if (attr._IsNullable != null) ret = attr;
131131
if (attr._IsIgnore != null) ret = attr;
132132
if (attr._IsVersion != null) ret = attr;
133-
if (!string.IsNullOrEmpty(attr.Unique)) ret = attr;
133+
if (attr._Uniques != null) ret = attr;
134134
if (attr.MapType != null) ret = attr;
135135
if (attr.DbDefautValue != null) ret = attr;
136136
if (ret != null && ret.MapType == null) ret.MapType = proto.PropertyType;

FreeSql/Internal/UtilsExpressionTree.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common) {
9191
if (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name;
9292
if (common.CodeFirst.IsSyncStructureToLower) {
9393
colattr.Name = colattr.Name.ToLower();
94-
if (!string.IsNullOrEmpty(colattr.Unique)) colattr.Unique = colattr.Unique.ToLower();
94+
colattr.Unique = colattr.Unique?.ToLower();
9595
}
9696
if (common.CodeFirst.IsSyncStructureToUpper) {
9797
colattr.Name = colattr.Name.ToUpper();
98-
if (!string.IsNullOrEmpty(colattr.Unique)) colattr.Unique = colattr.Unique.ToUpper();
98+
colattr.Unique = colattr.Unique?.ToUpper();
9999
}
100100

101101
if ((colattr.IsNullable != true || colattr.IsIdentity == true || colattr.IsPrimary == true) && colattr.DbType.Contains("NOT NULL") == false) {
@@ -179,7 +179,8 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common) {
179179
foreach (var dbcol in dbuk.Value) {
180180
if (trytb.Columns.TryGetValue(dbcol.Name, out var trycol) && trycol.Attribute.MapType == dbcol.CsType ||
181181
trytb.ColumnsByCs.TryGetValue(dbcol.Name, out trycol) && trycol.Attribute.MapType == dbcol.CsType) {
182-
trycol.Attribute.Unique = dbuk.Key;
182+
if (trycol.Attribute._Uniques?.Contains(dbuk.Key) != true)
183+
trycol.Attribute.Unique += $"," + dbuk.Key;
183184
}
184185
}
185186
}
@@ -188,8 +189,8 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common) {
188189
} catch { }
189190
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
190191
}
191-
trytb.Uniques = trytb.Columns.Values.Where(a => !string.IsNullOrEmpty(a.Attribute.Unique)).Select(a => a.Attribute.Unique).Distinct()
192-
.ToDictionary(a => a, a => trytb.Columns.Values.Where(b => b.Attribute.Unique == a).ToList());
192+
var allunique = trytb.Columns.Values.Where(a => a.Attribute._Uniques != null).SelectMany(a => a.Attribute._Uniques).Distinct();
193+
trytb.Uniques = allunique.ToDictionary(a => a, a => trytb.Columns.Values.Where(b => b.Attribute._Uniques != null && b.Attribute._Uniques.Contains(a)).ToList());
193194
tbc.AddOrUpdate(entity, trytb, (oldkey, oldval) => trytb);
194195

195196
#region 查找导航属性的关系、virtual 属性延时加载,动态产生新的重写类

0 commit comments

Comments
 (0)