- 补充 nuget 包增加 xmlDoc 编译;

- 调整 Column.Unique 定义规则,解决同一属性不可配置多次的问题;
This commit is contained in:
28810 2019-04-28 17:43:42 +08:00
parent d49be984bd
commit 163fe89bd4
10 changed files with 50 additions and 28 deletions

View File

@ -45,11 +45,11 @@ namespace FreeSql.Tests.MySql {
[Column(Unique = "uk_phone")] [Column(Unique = "uk_phone")]
public string phone { get; set; } public string phone { get; set; }
[Column(Unique = "uk_group_index")] [Column(Unique = "uk_group_index, uk_group_index22")]
public string group { get; set; } public string group { get; set; }
[Column(Unique = "uk_group_index11")] [Column(Unique = "uk_group_index")]
public int index { get; set; } public int index { get; set; }
[Column(Unique = "uk_group_index222")] [Column(Unique = "uk_group_index22")]
public string index22 { get; set; } public string index22 { get; set; }
} }

View File

@ -45,11 +45,11 @@ namespace FreeSql.Tests.Oracle {
[Column(Unique = "uk_phone")] [Column(Unique = "uk_phone")]
public string phone { get; set; } public string phone { get; set; }
[Column(Unique = "uk_group_index")] [Column(Unique = "uk_group_index, uk_group_index22")]
public string group { get; set; } public string group { get; set; }
[Column(Unique = "uk_group_index11")] [Column(Unique = "uk_group_index")]
public int index { get; set; } public int index { get; set; }
[Column(Unique = "uk_group_index222")] [Column(Unique = "uk_group_index22")]
public string index22 { get; set; } public string index22 { get; set; }
} }
[Fact] [Fact]

View File

@ -52,11 +52,11 @@ namespace FreeSql.Tests.PostgreSQL {
[Column(Unique = "uk_phone")] [Column(Unique = "uk_phone")]
public string phone { get; set; } public string phone { get; set; }
[Column(Unique = "uk_group_index")] [Column(Unique = "uk_group_index, uk_group_index22")]
public string group { get; set; } public string group { get; set; }
[Column(Unique = "uk_group_index11")] [Column(Unique = "uk_group_index")]
public int index { get; set; } public int index { get; set; }
[Column(Unique = "uk_group_index222")] [Column(Unique = "uk_group_index22")]
public string index22 { get; set; } public string index22 { get; set; }
} }

View File

@ -56,11 +56,11 @@ namespace FreeSql.Tests.SqlServer {
[Column(Unique = "uk_phone")] [Column(Unique = "uk_phone")]
public string phone { get; set; } public string phone { get; set; }
[Column(Unique = "uk_group_index")] [Column(Unique = "uk_group_index, uk_group_index22")]
public string group { get; set; } public string group { get; set; }
[Column(Unique = "uk_group_index11")] [Column(Unique = "uk_group_index")]
public int index { get; set; } public int index { get; set; }
[Column(Unique = "uk_group_index222")] [Column(Unique = "uk_group_index22")]
public string index22 { get; set; } public string index22 { get; set; }
} }

View File

@ -45,11 +45,11 @@ namespace FreeSql.Tests.Sqlite {
[Column(Unique = "uk_phone")] [Column(Unique = "uk_phone")]
public string phone { get; set; } public string phone { get; set; }
[Column(Unique = "uk_group_index")] [Column(Unique = "uk_group_index, uk_group_index22")]
public string group { get; set; } public string group { get; set; }
[Column(Unique = "uk_group_index111")] [Column(Unique = "uk_group_index")]
public int index { get; set; } public int index { get; set; }
[Column(Unique = "uk_group_index222")] [Column(Unique = "uk_group_index22")]
public string index22 { get; set; } public string index22 { get; set; }
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
namespace FreeSql.DataAnnotations { namespace FreeSql.DataAnnotations {
public class ColumnAttribute : Attribute { public class ColumnAttribute : Attribute {
@ -38,10 +39,30 @@ namespace FreeSql.DataAnnotations {
/// </summary> /// </summary>
public bool IsVersion { get => _IsVersion ?? false; set => _IsVersion = value; } public bool IsVersion { get => _IsVersion ?? false; set => _IsVersion = value; }
internal string[] _Uniques;
/// <summary> /// <summary>
/// 唯一键,多个属性指定相同的标识,代表联合键 /// 唯一键,多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。
/// </summary> /// </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>
/// 数据库默认值 /// 数据库默认值
/// </summary> /// </summary>

View File

@ -66,7 +66,7 @@ namespace FreeSql.DataAnnotations {
return this; return this;
} }
/// <summary> /// <summary>
/// 唯一键,多个属性指定相同的标识,代表联合键 /// 唯一键,多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。
/// </summary> /// </summary>
/// <param name="value">标识</param> /// <param name="value">标识</param>
/// <returns></returns> /// <returns></returns>

View File

@ -46,7 +46,7 @@
</member> </member>
<member name="P:FreeSql.DataAnnotations.ColumnAttribute.Unique"> <member name="P:FreeSql.DataAnnotations.ColumnAttribute.Unique">
<summary> <summary>
唯一键,多个属性指定相同的标识,代表联合键 唯一键,多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。
</summary> </summary>
</member> </member>
<member name="P:FreeSql.DataAnnotations.ColumnAttribute.DbDefautValue"> <member name="P:FreeSql.DataAnnotations.ColumnAttribute.DbDefautValue">
@ -101,7 +101,7 @@
</member> </member>
<member name="M:FreeSql.DataAnnotations.ColumnFluent.Unique(System.String)"> <member name="M:FreeSql.DataAnnotations.ColumnFluent.Unique(System.String)">
<summary> <summary>
唯一键,多个属性指定相同的标识,代表联合键 唯一键,多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。
</summary> </summary>
<param name="value">标识</param> <param name="value">标识</param>
<returns></returns> <returns></returns>

View File

@ -101,7 +101,7 @@ namespace FreeSql.Internal {
if (trycol._IsNullable != null) attr._IsNullable = trycol.IsNullable; if (trycol._IsNullable != null) attr._IsNullable = trycol.IsNullable;
if (trycol._IsIgnore != null) attr._IsIgnore = trycol.IsIgnore; if (trycol._IsIgnore != null) attr._IsIgnore = trycol.IsIgnore;
if (trycol._IsVersion != null) attr._IsVersion = trycol.IsVersion; 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.MapType != null) attr.MapType = trycol.MapType;
if (trycol.DbDefautValue != null) attr.DbDefautValue = trycol.DbDefautValue; if (trycol.DbDefautValue != null) attr.DbDefautValue = trycol.DbDefautValue;
} }
@ -117,7 +117,7 @@ namespace FreeSql.Internal {
if (tryattr._IsNullable != null) attr._IsNullable = tryattr.IsNullable; if (tryattr._IsNullable != null) attr._IsNullable = tryattr.IsNullable;
if (tryattr._IsIgnore != null) attr._IsIgnore = tryattr.IsIgnore; if (tryattr._IsIgnore != null) attr._IsIgnore = tryattr.IsIgnore;
if (tryattr._IsVersion != null) attr._IsVersion = tryattr.IsVersion; 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.MapType != null) attr.MapType = tryattr.MapType;
if (tryattr.DbDefautValue != null) attr.DbDefautValue = tryattr.DbDefautValue; if (tryattr.DbDefautValue != null) attr.DbDefautValue = tryattr.DbDefautValue;
} }
@ -130,7 +130,7 @@ namespace FreeSql.Internal {
if (attr._IsNullable != null) ret = attr; if (attr._IsNullable != null) ret = attr;
if (attr._IsIgnore != null) ret = attr; if (attr._IsIgnore != null) ret = attr;
if (attr._IsVersion != 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.MapType != null) ret = attr;
if (attr.DbDefautValue != null) ret = attr; if (attr.DbDefautValue != null) ret = attr;
if (ret != null && ret.MapType == null) ret.MapType = proto.PropertyType; if (ret != null && ret.MapType == null) ret.MapType = proto.PropertyType;

View File

@ -91,11 +91,11 @@ namespace FreeSql.Internal {
if (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name; if (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name;
if (common.CodeFirst.IsSyncStructureToLower) { if (common.CodeFirst.IsSyncStructureToLower) {
colattr.Name = colattr.Name.ToLower(); colattr.Name = colattr.Name.ToLower();
if (!string.IsNullOrEmpty(colattr.Unique)) colattr.Unique = colattr.Unique.ToLower(); colattr.Unique = colattr.Unique?.ToLower();
} }
if (common.CodeFirst.IsSyncStructureToUpper) { if (common.CodeFirst.IsSyncStructureToUpper) {
colattr.Name = colattr.Name.ToUpper(); 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) { if ((colattr.IsNullable != true || colattr.IsIdentity == true || colattr.IsPrimary == true) && colattr.DbType.Contains("NOT NULL") == false) {
@ -179,7 +179,8 @@ namespace FreeSql.Internal {
foreach (var dbcol in dbuk.Value) { foreach (var dbcol in dbuk.Value) {
if (trytb.Columns.TryGetValue(dbcol.Name, out var trycol) && trycol.Attribute.MapType == dbcol.CsType || 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) { 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;
} }
} }
} }
@ -188,8 +189,8 @@ namespace FreeSql.Internal {
} catch { } } catch { }
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray(); 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() var allunique = trytb.Columns.Values.Where(a => a.Attribute._Uniques != null).SelectMany(a => a.Attribute._Uniques).Distinct();
.ToDictionary(a => a, a => trytb.Columns.Values.Where(b => b.Attribute.Unique == a).ToList()); 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); tbc.AddOrUpdate(entity, trytb, (oldkey, oldval) => trytb);
#region virtual #region virtual