diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index a7a94fcda..c941a15d9 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -758,5 +758,13 @@ + + + 批量注入 Repository,可以参考代码自行调整 + + + + + diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index ca9726454..b202543c8 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -16,6 +16,7 @@ using System.Numerics; using System.Reflection; using System.Text; using System.Text.RegularExpressions; +using static System.Runtime.CompilerServices.RuntimeHelpers; namespace FreeSql.Internal { @@ -1003,6 +1004,7 @@ namespace FreeSql.Internal var midTypePropsTrytb = tbmid.Properties.Where(a => a.Value.PropertyType == trytb.Type).FirstOrDefault().Value; //g.mysql.Select().Where(a => g.mysql.Select().Where(b => b.Tag_id == a.Id && b.Song_id == 1).Any()); var lmbdWhere = isLazy ? new StringBuilder() : null; + var minPkCols = new List(); if (pnvAttr?.ManyToMany != null) { @@ -1039,6 +1041,8 @@ namespace FreeSql.Internal if (tbmid.Primarys.Any() == false) foreach (var c in trytbTf.Columns) tbmid.ColumnsByCs[c.CsName].Attribute.IsPrimary = true; + else + minPkCols.AddRange(trytbTf.Columns); if (isLazy) { @@ -1086,6 +1090,8 @@ namespace FreeSql.Internal if (tbmid.Primarys.Any() == false) foreach (var c in tbrefTf.Columns) tbmid.ColumnsByCs[c.CsName].Attribute.IsPrimary = true; + else + minPkCols.AddRange(tbrefTf.Columns); if (isLazy) { @@ -1129,6 +1135,8 @@ namespace FreeSql.Internal nvref.MiddleColumns.Add(trycol); if (tbmid.Primarys.Any() == false) trycol.Attribute.IsPrimary = true; + else + minPkCols.Add(trycol); if (isLazy) { @@ -1169,6 +1177,8 @@ namespace FreeSql.Internal nvref.MiddleColumns.Add(trycol); if (tbmid.Primarys.Any() == false) trycol.Attribute.IsPrimary = true; + else + minPkCols.Add(trycol); if (isLazy) lmbdWhere.Append(" && b.").Append(trycol.CsName).Append(" == a.").Append(tbref.Primarys[a].CsName); } @@ -1197,6 +1207,11 @@ namespace FreeSql.Internal } } } + else if(minPkCols.Any(c => tbmid.ColumnsByCs[c.CsName].Attribute.IsPrimary == false)) + { + nvref.Exception = new Exception($"导航属性 {trytbTypeName}.{pnv.Name} 解析错误,中间类主键错误:{midType.Name}({string.Join(",", tbmid.Primarys.Select(a => a.CsName))}) 与两边不匹配"); + trytb.AddOrUpdateTableRef(pnv.Name, nvref); + } } if (isLazy) diff --git a/Providers/FreeSql.Provider.Custom/PostgreSQL/CustomPostgreSQLCodeFirst.cs b/Providers/FreeSql.Provider.Custom/PostgreSQL/CustomPostgreSQLCodeFirst.cs index bb5a77f26..2cc8b5e73 100644 --- a/Providers/FreeSql.Provider.Custom/PostgreSQL/CustomPostgreSQLCodeFirst.cs +++ b/Providers/FreeSql.Provider.Custom/PostgreSQL/CustomPostgreSQLCodeFirst.cs @@ -32,7 +32,7 @@ namespace FreeSql.Custom.PostgreSQL { typeof(decimal).FullName, CsToDb.New(DbType.Decimal, "numeric", "numeric(10,2) NOT NULL", false, false, 0) },{ typeof(decimal?).FullName, CsToDb.New(DbType.Decimal, "numeric", "numeric(10,2)", false, true, null) }, { typeof(string).FullName, CsToDb.New(DbType.String, "varchar", "varchar(255)", false, null, "") }, - { typeof(char).FullName, CsToDb.New(DbType.AnsiString, "char", "char(1) NULL", false, null, '\0') }, + { typeof(char).FullName, CsToDb.New(DbType.AnsiString, "bpchar", "bpchar(1) NULL", false, null, '\0') }, { typeof(TimeSpan).FullName, CsToDb.New(DbType.Time, "time","time NOT NULL", false, false, 0) },{ typeof(TimeSpan?).FullName, CsToDb.New(DbType.Time, "time", "time",false, true, null) }, { typeof(DateTime).FullName, CsToDb.New(DbType.DateTime, "timestamp", "timestamp NOT NULL", false, false, new DateTime(1970,1,1)) },{ typeof(DateTime?).FullName, CsToDb.New(DbType.DateTime, "timestamp", "timestamp", false, true, null) }, diff --git a/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs b/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs index 173f1b92e..14b5d570d 100644 --- a/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs +++ b/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs @@ -33,7 +33,7 @@ namespace FreeSql.Odbc.PostgreSQL { typeof(decimal).FullName, CsToDb.New(OdbcType.Numeric, "numeric", "numeric(10,2) NOT NULL", false, false, 0) },{ typeof(decimal?).FullName, CsToDb.New(OdbcType.Numeric, "numeric", "numeric(10,2)", false, true, null) }, { typeof(string).FullName, CsToDb.New(OdbcType.VarChar, "varchar", "varchar(255)", false, null, "") }, - { typeof(char).FullName, CsToDb.New(OdbcType.Char, "char", "char(1) NULL", false, null, '\0') }, + { typeof(char).FullName, CsToDb.New(OdbcType.Char, "bpchar", "bpchar(1) NULL", false, null, '\0') }, { typeof(TimeSpan).FullName, CsToDb.New(OdbcType.Time, "time","time NOT NULL", false, false, 0) },{ typeof(TimeSpan?).FullName, CsToDb.New(OdbcType.Time, "time", "time",false, true, null) }, { typeof(DateTime).FullName, CsToDb.New(OdbcType.DateTime, "timestamp", "timestamp NOT NULL", false, false, new DateTime(1970,1,1)) },{ typeof(DateTime?).FullName, CsToDb.New(OdbcType.DateTime, "timestamp", "timestamp", false, true, null) },