diff --git a/FreeSql.Tests/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs b/FreeSql.Tests/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs
index d91dc319..c3dd0d2b 100644
--- a/FreeSql.Tests/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs
+++ b/FreeSql.Tests/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs
@@ -351,7 +351,7 @@ namespace FreeSql.Tests.PostgreSQL
[Table(Name = "tb_alltype")]
class TableAllType
{
- [Column(IsIdentity = true, IsPrimary = true)]
+ [Column(IsPrimary = true)]
public int Id { get; set; }
public bool testFieldBool { get; set; }
diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml
index 53d85f09..6179f8fa 100644
--- a/FreeSql/FreeSql.xml
+++ b/FreeSql/FreeSql.xml
@@ -2185,6 +2185,11 @@
实体配置
+
+
+ 索引配置
+
+
实体类型
diff --git a/FreeSql/Interface/IAop.cs b/FreeSql/Interface/IAop.cs
index c1c5c3f4..ce18c3ae 100644
--- a/FreeSql/Interface/IAop.cs
+++ b/FreeSql/Interface/IAop.cs
@@ -115,6 +115,7 @@ namespace FreeSql.Aop
{
this.EntityType = entityType;
this.ModifyResult = new TableAttribute();
+ this.ModifyIndexResult = new List();
}
///
@@ -125,6 +126,10 @@ namespace FreeSql.Aop
/// 实体配置
///
public TableAttribute ModifyResult { get; }
+ ///
+ /// 索引配置
+ ///
+ public List ModifyIndexResult { get; }
}
public class ConfigEntityPropertyEventArgs : EventArgs
{
diff --git a/FreeSql/Internal/CommonUtils.cs b/FreeSql/Internal/CommonUtils.cs
index 7c9ed240..c19b4c28 100644
--- a/FreeSql/Internal/CommonUtils.cs
+++ b/FreeSql/Internal/CommonUtils.cs
@@ -87,7 +87,6 @@ namespace FreeSql.Internal
if (!string.IsNullOrEmpty(trytb.OldName)) attr.OldName = trytb.OldName;
if (!string.IsNullOrEmpty(trytb.SelectFilter)) attr.SelectFilter = trytb.SelectFilter;
if (trytb._DisableSyncStructure != null) attr._DisableSyncStructure = trytb.DisableSyncStructure;
-
}
var attrs = type.GetCustomAttributes(typeof(TableAttribute), false);
foreach (var tryattrobj in attrs)
@@ -190,24 +189,37 @@ namespace FreeSql.Internal
}
public IndexAttribute[] GetEntityIndexAttribute(Type type)
{
- var ret = new Dictionary(); ;
+ var ret = new Dictionary();
+ if (_orm.Aop.ConfigEntity != null)
+ {
+ var aope = new Aop.ConfigEntityEventArgs(type);
+ _orm.Aop.ConfigEntity(_orm, aope);
+ foreach (var idxattr in aope.ModifyIndexResult)
+ if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields))
+ {
+ if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name);
+ ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique });
+ }
+ }
if (dicConfigEntity.TryGetValue(type, out var trytb))
{
foreach (var idxattr in trytb._indexs.Values)
- {
if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields))
+ {
+ if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name);
ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique });
- }
+ }
}
var attrs = type.GetCustomAttributes(typeof(IndexAttribute), true);
foreach (var tryattrobj in attrs)
{
var idxattr = tryattrobj as IndexAttribute;
if (idxattr == null) continue;
- if (string.IsNullOrEmpty(idxattr.Name)) continue;
- if (string.IsNullOrEmpty(idxattr.Fields)) continue;
- if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name);
- ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique });
+ if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields))
+ {
+ if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name);
+ ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique });
+ }
}
return ret.Values.ToArray();
}
diff --git a/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs b/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs
index fdd063a8..2d04cd90 100644
--- a/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs
+++ b/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs
@@ -165,7 +165,8 @@ t.typname,
case when a.atttypmod > 0 and a.atttypmod < 32767 then a.atttypmod - 4 else a.attlen end len,
case when t.typelem > 0 and t.typinput::varchar = 'array_in' then t2.typname else t.typname end,
case when a.attnotnull then '0' else '1' end as is_nullable,
-e.adsrc,
+coalesce((select 1 from pg_sequences where sequencename = {0} || '_' || {1} || '_' || a.attname || '_sequence_name' limit 1),0) is_identity,
+--e.adsrc,
a.attndims,
d.description as comment
from pg_class c
@@ -201,7 +202,7 @@ where ns.nspname = {0} and c.relname = {1}", tboldname ?? tbname);
sqlType = string.Concat(sqlType),
max_length = long.Parse(string.Concat(a[2])),
is_nullable = string.Concat(a[4]) == "1",
- is_identity = string.Concat(a[5]).StartsWith(@"nextval('") && string.Concat(a[5]).EndsWith(@"'::regclass)"),
+ is_identity = string.Concat(a[5]) == "1", //string.Concat(a[5]).StartsWith(@"nextval('") && string.Concat(a[5]).EndsWith(@"'::regclass)"),
attndims,
comment = string.Concat(a[7])
};
diff --git a/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLDbFirst.cs b/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLDbFirst.cs
index 148d9d0d..d4229572 100644
--- a/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLDbFirst.cs
+++ b/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLDbFirst.cs
@@ -208,8 +208,8 @@ t.typname,
case when a.atttypmod > 0 and a.atttypmod < 32767 then a.atttypmod - 4 else a.attlen end len,
case when t.typelem = 0 then t.typname else t2.typname end,
case when a.attnotnull then 0 else 1 end as is_nullable,
-e.adsrc as is_identity,
---case when e.adsrc = 'nextval(''' || case when ns.nspname = 'public' then '' else ns.nspname || '.' end || c.relname || '_' || a.attname || '_seq''::regclass)' then 1 else 0 end as is_identity,
+coalesce((select 1 from pg_sequences where sequencename = {0} || '_' || {1} || '_' || a.attname || '_sequence_name' limit 1),0) is_identity,
+--e.adsrc as is_identity,
d.description as comment,
a.attndims,
case when t.typelem = 0 then t.typtype else t2.typtype end,
@@ -235,7 +235,7 @@ where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || c.relname")
var max_length = int.Parse(string.Concat(row[3]));
var sqlType = string.Concat(row[4]);
var is_nullable = string.Concat(row[5]) == "1";
- var is_identity = string.Concat(row[6]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"'::regclass)");
+ var is_identity = string.Concat(row[6]) == "1"; //string.Concat(row[6]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"'::regclass)");
var comment = string.Concat(row[7]);
int attndims = int.Parse(string.Concat(row[8]));
string typtype = string.Concat(row[9]);
diff --git a/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs b/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs
index 1fd94504..961ef8b5 100644
--- a/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs
+++ b/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs
@@ -207,7 +207,8 @@ t.typname,
case when a.atttypmod > 0 and a.atttypmod < 32767 then a.atttypmod - 4 else a.attlen end len,
case when t.typelem > 0 and t.typinput::varchar = 'array_in' then t2.typname else t.typname end,
case when a.attnotnull then '0' else '1' end as is_nullable,
-e.adsrc,
+coalesce((select 1 from pg_sequences where sequencename = {0} || '_' || {1} || '_' || a.attname || '_sequence_name' limit 1),0) is_identity,
+--e.adsrc,
a.attndims,
d.description as comment
from pg_class c
@@ -243,7 +244,7 @@ where ns.nspname = {0} and c.relname = {1}", tboldname ?? tbname);
sqlType = string.Concat(sqlType),
max_length = long.Parse(string.Concat(a[2])),
is_nullable = string.Concat(a[4]) == "1",
- is_identity = string.Concat(a[5]).StartsWith(@"nextval('") && string.Concat(a[5]).EndsWith(@"'::regclass)"),
+ is_identity = string.Concat(a[5]) == "1", //string.Concat(a[5]).StartsWith(@"nextval('") && string.Concat(a[5]).EndsWith(@"'::regclass)"),
attndims,
comment = string.Concat(a[7])
};
diff --git a/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLDbFirst.cs b/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLDbFirst.cs
index 97734719..5910c1d3 100644
--- a/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLDbFirst.cs
+++ b/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLDbFirst.cs
@@ -319,8 +319,8 @@ t.typname,
case when a.atttypmod > 0 and a.atttypmod < 32767 then a.atttypmod - 4 else a.attlen end len,
case when t.typelem = 0 then t.typname else t2.typname end,
case when a.attnotnull then 0 else 1 end as is_nullable,
-e.adsrc as is_identity,
---case when e.adsrc = 'nextval(''' || case when ns.nspname = 'public' then '' else ns.nspname || '.' end || c.relname || '_' || a.attname || '_seq''::regclass)' then 1 else 0 end as is_identity,
+coalesce((select 1 from pg_sequences where sequencename = {0} || '_' || {1} || '_' || a.attname || '_sequence_name' limit 1),0) is_identity,
+--e.adsrc as is_identity,
d.description as comment,
a.attndims,
case when t.typelem = 0 then t.typtype else t2.typtype end,
@@ -346,7 +346,7 @@ where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || c.relname")
var max_length = int.Parse(string.Concat(row[3]));
var sqlType = string.Concat(row[4]);
var is_nullable = string.Concat(row[5]) == "1";
- var is_identity = string.Concat(row[6]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"'::regclass)");
+ var is_identity = string.Concat(row[6]) == "1"; //string.Concat(row[6]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"'::regclass)");
var comment = string.Concat(row[7]);
int attndims = int.Parse(string.Concat(row[8]));
string typtype = string.Concat(row[9]);