From 430618dcb7caa7f53d31ebe1b8ed5189cedbbe2b Mon Sep 17 00:00:00 2001
From: 28810 <28810@YEXIANGQIN>
Date: Thu, 1 Aug 2019 18:29:54 +0800
Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20FreeSql.Provider.Oracl?=
=?UTF-8?q?e=20=E4=B8=8B=E7=9A=84=20OraclePrimaryKeyName=20=E7=89=B9?=
=?UTF-8?q?=E6=80=A7=EF=BC=8C=E6=89=8B=E5=B7=A5=E8=AE=BE=E7=BD=AE=E4=B8=BB?=
=?UTF-8?q?=E9=94=AE=E5=90=8D=E9=98=B2=E6=AD=A2=E9=BB=98=E8=AE=A4=E5=90=8D?=
=?UTF-8?q?=E8=BF=87=E9=95=BF=E9=97=AE=E9=A2=98=EF=BC=9B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../OraclePrimaryKeyNameAttribute.cs | 17 +++++++++++++++++
.../FreeSql.Provider.Oracle/OracleCodeFirst.cs | 8 ++++++--
2 files changed, 23 insertions(+), 2 deletions(-)
create mode 100644 Providers/FreeSql.Provider.Oracle/DataAnnotations/OraclePrimaryKeyNameAttribute.cs
diff --git a/Providers/FreeSql.Provider.Oracle/DataAnnotations/OraclePrimaryKeyNameAttribute.cs b/Providers/FreeSql.Provider.Oracle/DataAnnotations/OraclePrimaryKeyNameAttribute.cs
new file mode 100644
index 00000000..fd2aa735
--- /dev/null
+++ b/Providers/FreeSql.Provider.Oracle/DataAnnotations/OraclePrimaryKeyNameAttribute.cs
@@ -0,0 +1,17 @@
+using System;
+
+namespace FreeSql.DataAnnotations
+{
+ public class OraclePrimaryKeyNameAttribute : Attribute
+ {
+ public OraclePrimaryKeyNameAttribute(string name)
+ {
+ this.Name = name;
+ }
+
+ ///
+ /// 主键名
+ ///
+ public string Name { get; set; }
+ }
+}
diff --git a/Providers/FreeSql.Provider.Oracle/OracleCodeFirst.cs b/Providers/FreeSql.Provider.Oracle/OracleCodeFirst.cs
index 6f6dbf94..daf4d3f4 100644
--- a/Providers/FreeSql.Provider.Oracle/OracleCodeFirst.cs
+++ b/Providers/FreeSql.Provider.Oracle/OracleCodeFirst.cs
@@ -8,6 +8,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
+using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
@@ -90,6 +91,7 @@ namespace FreeSql.Oracle
var tboldname = tb.DbOldName?.Split(new[] { '.' }, 2); //旧表名
if (tboldname?.Length == 1) tboldname = new[] { userId, tboldname[0] };
+ var primaryKeyName = entityType.GetCustomAttribute()?.Name;
if (string.Compare(tbname[0], userId) != 0 && _orm.Ado.ExecuteScalar(CommandType.Text, _commonUtils.FormatSql(" select 1 from sys.dba_users where username={0}", tbname[0])) == null) //创建数据库
throw new NotImplementedException($"Oracle CodeFirst 不支持代码创建 tablespace 与 schemas {tbname[0]}");
@@ -115,7 +117,8 @@ namespace FreeSql.Oracle
}
if (tb.Primarys.Any())
{
- sb.Append(" \r\n CONSTRAINT ").Append(tbname[0]).Append("_").Append(tbname[1]).Append("_pk1 PRIMARY KEY (");
+ var pkname = primaryKeyName ?? $"{tbname[0]}_{tbname[1]}_pk1";
+ sb.Append(" \r\n CONSTRAINT ").Append(pkname).Append(" PRIMARY KEY (");
foreach (var tbcol in tb.Primarys) sb.Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(", ");
sb.Remove(sb.Length - 2, 2).Append("),");
}
@@ -268,7 +271,8 @@ and a.owner in ({0}) and a.table_name in ({1})", tboldname ?? tbname);
}
if (tb.Primarys.Any())
{
- sb.Append(" \r\n CONSTRAINT ").Append(tbname[0]).Append("_").Append(tbname[1]).Append("_pk2 PRIMARY KEY (");
+ var pkname = primaryKeyName ?? $"{tbname[0]}_{tbname[1]}_pk2";
+ sb.Append(" \r\n CONSTRAINT ").Append(pkname).Append(" PRIMARY KEY (");
foreach (var tbcol in tb.Primarys) sb.Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(", ");
sb.Remove(sb.Length - 2, 2).Append("),");
}