Skip to content

Commit

Permalink
- 补充 nuget 包增加 xmlDoc 编译;
Browse files Browse the repository at this point in the history
- 调整 Column.Unique 定义规则,解决同一属性不可配置多次的问题;
  • Loading branch information
28810 authored and 28810 committed Apr 28, 2019
1 parent d49be98 commit 163fe89
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 28 deletions.
6 changes: 3 additions & 3 deletions FreeSql.Tests/MySql/MySqlCodeFirstTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ class AddUniquesInfo {
[Column(Unique = "uk_phone")]
public string phone { get; set; }

[Column(Unique = "uk_group_index")]
[Column(Unique = "uk_group_index, uk_group_index22")]
public string group { get; set; }
[Column(Unique = "uk_group_index11")]
[Column(Unique = "uk_group_index")]
public int index { get; set; }
[Column(Unique = "uk_group_index222")]
[Column(Unique = "uk_group_index22")]
public string index22 { get; set; }
}

Expand Down
6 changes: 3 additions & 3 deletions FreeSql.Tests/Oracle/OracleCodeFirstTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ class AddUniquesInfo {
[Column(Unique = "uk_phone")]
public string phone { get; set; }

[Column(Unique = "uk_group_index")]
[Column(Unique = "uk_group_index, uk_group_index22")]
public string group { get; set; }
[Column(Unique = "uk_group_index11")]
[Column(Unique = "uk_group_index")]
public int index { get; set; }
[Column(Unique = "uk_group_index222")]
[Column(Unique = "uk_group_index22")]
public string index22 { get; set; }
}
[Fact]
Expand Down
6 changes: 3 additions & 3 deletions FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class AddUniquesInfo {
[Column(Unique = "uk_phone")]
public string phone { get; set; }

[Column(Unique = "uk_group_index")]
[Column(Unique = "uk_group_index, uk_group_index22")]
public string group { get; set; }
[Column(Unique = "uk_group_index11")]
[Column(Unique = "uk_group_index")]
public int index { get; set; }
[Column(Unique = "uk_group_index222")]
[Column(Unique = "uk_group_index22")]
public string index22 { get; set; }
}

Expand Down
6 changes: 3 additions & 3 deletions FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ class AddUniquesInfo {
[Column(Unique = "uk_phone")]
public string phone { get; set; }

[Column(Unique = "uk_group_index")]
[Column(Unique = "uk_group_index, uk_group_index22")]
public string group { get; set; }
[Column(Unique = "uk_group_index11")]
[Column(Unique = "uk_group_index")]
public int index { get; set; }
[Column(Unique = "uk_group_index222")]
[Column(Unique = "uk_group_index22")]
public string index22 { get; set; }
}

Expand Down
6 changes: 3 additions & 3 deletions FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ class AddUniquesInfo {
[Column(Unique = "uk_phone")]
public string phone { get; set; }

[Column(Unique = "uk_group_index")]
[Column(Unique = "uk_group_index, uk_group_index22")]
public string group { get; set; }
[Column(Unique = "uk_group_index111")]
[Column(Unique = "uk_group_index")]
public int index { get; set; }
[Column(Unique = "uk_group_index222")]
[Column(Unique = "uk_group_index22")]
public string index22 { get; set; }
}

Expand Down
25 changes: 23 additions & 2 deletions FreeSql/DataAnnotations/ColumnAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

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

internal string[] _Uniques;
/// <summary>
/// 唯一键,多个属性指定相同的标识,代表联合键
/// 唯一键,在多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。
/// </summary>
public string Unique { get; set; }
public string Unique {
get => _Uniques == null ? null : string.Join(", ", _Uniques);
set {
if (string.IsNullOrEmpty(value)) {
_Uniques = null;
return;
}
var val = value?.Trim(' ', '\t', ',');
if (string.IsNullOrEmpty(val)) {
_Uniques = null;
return;
}
var arr = val.Split(',').Select(a => a.Trim(' ', '\t').Trim()).Where(a => !string.IsNullOrEmpty(a)).ToArray();
if (arr.Any() == false) {
_Uniques = null;
return;
}
_Uniques = arr;
}
}
/// <summary>
/// 数据库默认值
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion FreeSql/DataAnnotations/ColumnFluent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ColumnFluent IsVersion(bool value) {
return this;
}
/// <summary>
/// 唯一键,多个属性指定相同的标识,代表联合键
/// 唯一键,在多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。
/// </summary>
/// <param name="value">标识</param>
/// <returns></returns>
Expand Down
4 changes: 2 additions & 2 deletions FreeSql/FreeSql.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions FreeSql/Internal/CommonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ internal ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto)
if (trycol._IsNullable != null) attr._IsNullable = trycol.IsNullable;
if (trycol._IsIgnore != null) attr._IsIgnore = trycol.IsIgnore;
if (trycol._IsVersion != null) attr._IsVersion = trycol.IsVersion;
if (!string.IsNullOrEmpty(trycol.Unique)) attr.Unique = trycol.Unique;
if (trycol._Uniques != null) attr._Uniques = trycol._Uniques;
if (trycol.MapType != null) attr.MapType = trycol.MapType;
if (trycol.DbDefautValue != null) attr.DbDefautValue = trycol.DbDefautValue;
}
Expand All @@ -117,7 +117,7 @@ internal ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto)
if (tryattr._IsNullable != null) attr._IsNullable = tryattr.IsNullable;
if (tryattr._IsIgnore != null) attr._IsIgnore = tryattr.IsIgnore;
if (tryattr._IsVersion != null) attr._IsVersion = tryattr.IsVersion;
if (!string.IsNullOrEmpty(tryattr.Unique)) attr.Unique = tryattr.Unique;
if (tryattr._Uniques != null) attr._Uniques = tryattr._Uniques;
if (tryattr.MapType != null) attr.MapType = tryattr.MapType;
if (tryattr.DbDefautValue != null) attr.DbDefautValue = tryattr.DbDefautValue;
}
Expand All @@ -130,7 +130,7 @@ internal ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto)
if (attr._IsNullable != null) ret = attr;
if (attr._IsIgnore != null) ret = attr;
if (attr._IsVersion != null) ret = attr;
if (!string.IsNullOrEmpty(attr.Unique)) ret = attr;
if (attr._Uniques != null) ret = attr;
if (attr.MapType != null) ret = attr;
if (attr.DbDefautValue != null) ret = attr;
if (ret != null && ret.MapType == null) ret.MapType = proto.PropertyType;
Expand Down
11 changes: 6 additions & 5 deletions FreeSql/Internal/UtilsExpressionTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common) {
if (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name;
if (common.CodeFirst.IsSyncStructureToLower) {
colattr.Name = colattr.Name.ToLower();
if (!string.IsNullOrEmpty(colattr.Unique)) colattr.Unique = colattr.Unique.ToLower();
colattr.Unique = colattr.Unique?.ToLower();
}
if (common.CodeFirst.IsSyncStructureToUpper) {
colattr.Name = colattr.Name.ToUpper();
if (!string.IsNullOrEmpty(colattr.Unique)) colattr.Unique = colattr.Unique.ToUpper();
colattr.Unique = colattr.Unique?.ToUpper();
}

if ((colattr.IsNullable != true || colattr.IsIdentity == true || colattr.IsPrimary == true) && colattr.DbType.Contains("NOT NULL") == false) {
Expand Down Expand Up @@ -179,7 +179,8 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common) {
foreach (var dbcol in dbuk.Value) {
if (trytb.Columns.TryGetValue(dbcol.Name, out var trycol) && trycol.Attribute.MapType == dbcol.CsType ||
trytb.ColumnsByCs.TryGetValue(dbcol.Name, out trycol) && trycol.Attribute.MapType == dbcol.CsType) {
trycol.Attribute.Unique = dbuk.Key;
if (trycol.Attribute._Uniques?.Contains(dbuk.Key) != true)
trycol.Attribute.Unique += $"," + dbuk.Key;
}
}
}
Expand All @@ -188,8 +189,8 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common) {
} catch { }
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
}
trytb.Uniques = trytb.Columns.Values.Where(a => !string.IsNullOrEmpty(a.Attribute.Unique)).Select(a => a.Attribute.Unique).Distinct()
.ToDictionary(a => a, a => trytb.Columns.Values.Where(b => b.Attribute.Unique == a).ToList());
var allunique = trytb.Columns.Values.Where(a => a.Attribute._Uniques != null).SelectMany(a => a.Attribute._Uniques).Distinct();
trytb.Uniques = allunique.ToDictionary(a => a, a => trytb.Columns.Values.Where(b => b.Attribute._Uniques != null && b.Attribute._Uniques.Contains(a)).ToList());
tbc.AddOrUpdate(entity, trytb, (oldkey, oldval) => trytb);

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

0 comments on commit 163fe89

Please sign in to comment.