From 163fe89bd439e7b3309744d52c3d3136cdf7d086 Mon Sep 17 00:00:00 2001
From: 28810 <28810@YEXIANGQIN>
Date: Sun, 28 Apr 2019 17:43:42 +0800
Subject: [PATCH] =?UTF-8?q?-=20=E8=A1=A5=E5=85=85=20nuget=20=E5=8C=85?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0=20xmlDoc=20=E7=BC=96=E8=AF=91=EF=BC=9B=20-?=
=?UTF-8?q?=20=E8=B0=83=E6=95=B4=20Column.Unique=20=E5=AE=9A=E4=B9=89?=
=?UTF-8?q?=E8=A7=84=E5=88=99=EF=BC=8C=E8=A7=A3=E5=86=B3=E5=90=8C=E4=B8=80?=
=?UTF-8?q?=E5=B1=9E=E6=80=A7=E4=B8=8D=E5=8F=AF=E9=85=8D=E7=BD=AE=E5=A4=9A?=
=?UTF-8?q?=E6=AC=A1=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FreeSql.Tests/MySql/MySqlCodeFirstTest.cs | 6 ++---
FreeSql.Tests/Oracle/OracleCodeFirstTest.cs | 6 ++---
.../PostgreSQL/PostgreSQLCodeFirstTest.cs | 6 ++---
.../SqlServer/SqlServerCodeFirstTest.cs | 6 ++---
FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs | 6 ++---
FreeSql/DataAnnotations/ColumnAttribute.cs | 25 +++++++++++++++++--
FreeSql/DataAnnotations/ColumnFluent.cs | 2 +-
FreeSql/FreeSql.xml | 4 +--
FreeSql/Internal/CommonUtils.cs | 6 ++---
FreeSql/Internal/UtilsExpressionTree.cs | 11 ++++----
10 files changed, 50 insertions(+), 28 deletions(-)
diff --git a/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs b/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs
index 4e546f8b5..90673eca4 100644
--- a/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs
+++ b/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs
@@ -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; }
}
diff --git a/FreeSql.Tests/Oracle/OracleCodeFirstTest.cs b/FreeSql.Tests/Oracle/OracleCodeFirstTest.cs
index 8fbe5f200..5bf2a0d33 100644
--- a/FreeSql.Tests/Oracle/OracleCodeFirstTest.cs
+++ b/FreeSql.Tests/Oracle/OracleCodeFirstTest.cs
@@ -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]
diff --git a/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs b/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs
index b8cb972c7..423dd33c7 100644
--- a/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs
+++ b/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs
@@ -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; }
}
diff --git a/FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs b/FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs
index 23227114d..7d097c0d8 100644
--- a/FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs
+++ b/FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs
@@ -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; }
}
diff --git a/FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs b/FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs
index ae7389e99..b6a3e8bd4 100644
--- a/FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs
+++ b/FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs
@@ -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; }
}
diff --git a/FreeSql/DataAnnotations/ColumnAttribute.cs b/FreeSql/DataAnnotations/ColumnAttribute.cs
index c3b4d0188..df5c98353 100644
--- a/FreeSql/DataAnnotations/ColumnAttribute.cs
+++ b/FreeSql/DataAnnotations/ColumnAttribute.cs
@@ -1,4 +1,5 @@
using System;
+using System.Linq;
namespace FreeSql.DataAnnotations {
public class ColumnAttribute : Attribute {
@@ -38,10 +39,30 @@ public class ColumnAttribute : Attribute {
///
public bool IsVersion { get => _IsVersion ?? false; set => _IsVersion = value; }
+ internal string[] _Uniques;
///
- /// 唯一键,多个属性指定相同的标识,代表联合键
+ /// 唯一键,在多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。
///
- 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;
+ }
+ }
///
/// 数据库默认值
///
diff --git a/FreeSql/DataAnnotations/ColumnFluent.cs b/FreeSql/DataAnnotations/ColumnFluent.cs
index 8fa9c6dc8..6894cde17 100644
--- a/FreeSql/DataAnnotations/ColumnFluent.cs
+++ b/FreeSql/DataAnnotations/ColumnFluent.cs
@@ -66,7 +66,7 @@ public ColumnFluent IsVersion(bool value) {
return this;
}
///
- /// 唯一键,多个属性指定相同的标识,代表联合键
+ /// 唯一键,在多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。
///
/// 标识
///
diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml
index 0a5b20d3d..f45283ac9 100644
--- a/FreeSql/FreeSql.xml
+++ b/FreeSql/FreeSql.xml
@@ -46,7 +46,7 @@
- 唯一键,多个属性指定相同的标识,代表联合键
+ 唯一键,在多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。
@@ -101,7 +101,7 @@
- 唯一键,多个属性指定相同的标识,代表联合键
+ 唯一键,在多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。
标识
diff --git a/FreeSql/Internal/CommonUtils.cs b/FreeSql/Internal/CommonUtils.cs
index fe9ef4f95..91f323082 100644
--- a/FreeSql/Internal/CommonUtils.cs
+++ b/FreeSql/Internal/CommonUtils.cs
@@ -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;
}
@@ -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;
}
@@ -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;
diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs
index 316600386..79b4202e9 100644
--- a/FreeSql/Internal/UtilsExpressionTree.cs
+++ b/FreeSql/Internal/UtilsExpressionTree.cs
@@ -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) {
@@ -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;
}
}
}
@@ -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 属性延时加载,动态产生新的重写类