mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 12:28:15 +08:00
- 增加 StringLength/MaxLength 对 byte[] 的支持;
This commit is contained in:
@ -79,7 +79,7 @@ namespace FreeSql.DataAnnotations
|
||||
|
||||
internal int? _StringLength;
|
||||
/// <summary>
|
||||
/// 设置长度,针对 string 类型避免 DbType 的繁琐设置<para></para>
|
||||
/// 设置长度,针对 string/byte[] 类型避免 DbType 的繁琐设置<para></para>
|
||||
/// 提示:也可以使用 [MaxLength(100)]<para></para>
|
||||
/// ---<para></para>
|
||||
/// StringLength = 100 时,对应 DbType:<para></para>
|
||||
@ -90,11 +90,12 @@ namespace FreeSql.DataAnnotations
|
||||
/// Sqlite -> nvarchar(100)<para></para>
|
||||
/// ---<para></para>
|
||||
/// StringLength < 0 时,对应 DbType:<para></para>
|
||||
/// MySql -> text (StringLength = -2 时,对应 DbType longtext)<para></para>
|
||||
/// MySql -> text (StringLength = -2 时,对应 longtext)<para></para>
|
||||
/// SqlServer -> nvarchar(max)<para></para>
|
||||
/// PostgreSQL -> text<para></para>
|
||||
/// Oracle -> nclob<para></para>
|
||||
/// Sqlite -> text<para></para>
|
||||
/// v1.6.0+ byte[] 支持设置 StringLength
|
||||
/// </summary>
|
||||
public int StringLength { get => _StringLength ?? 0; set => _StringLength = value; }
|
||||
|
||||
|
@ -80,7 +80,7 @@
|
||||
</member>
|
||||
<member name="P:FreeSql.DataAnnotations.ColumnAttribute.StringLength">
|
||||
<summary>
|
||||
设置长度,针对 string 类型避免 DbType 的繁琐设置<para></para>
|
||||
设置长度,针对 string/byte[] 类型避免 DbType 的繁琐设置<para></para>
|
||||
提示:也可以使用 [MaxLength(100)]<para></para>
|
||||
---<para></para>
|
||||
StringLength = 100 时,对应 DbType:<para></para>
|
||||
@ -91,11 +91,12 @@
|
||||
Sqlite -> nvarchar(100)<para></para>
|
||||
---<para></para>
|
||||
StringLength < 0 时,对应 DbType:<para></para>
|
||||
MySql -> text (StringLength = -2 时,对应 DbType longtext)<para></para>
|
||||
MySql -> text (StringLength = -2 时,对应 longtext)<para></para>
|
||||
SqlServer -> nvarchar(max)<para></para>
|
||||
PostgreSQL -> text<para></para>
|
||||
Oracle -> nclob<para></para>
|
||||
Sqlite -> text<para></para>
|
||||
v1.6.0+ byte[] 支持设置 StringLength
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:FreeSql.DataAnnotations.ColumnAttribute.InsertValueSql">
|
||||
|
@ -272,6 +272,48 @@ namespace FreeSql.Internal
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (colattr.MapType == typeof(byte[]) && colattr.StringLength != 0)
|
||||
{
|
||||
int strlen = colattr.StringLength;
|
||||
var charPatten = @"(VARBINARY|BINARY|BYTEA)\s*(\([^\)]*\))?";
|
||||
switch (common._orm.Ado.DataType)
|
||||
{
|
||||
case DataType.MySql:
|
||||
case DataType.OdbcMySql:
|
||||
if (strlen == -2) colattr.DbType = "LONGBLOB";
|
||||
else if (strlen < 0) colattr.DbType = "BLOB";
|
||||
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
|
||||
break;
|
||||
case DataType.SqlServer:
|
||||
case DataType.OdbcSqlServer:
|
||||
if (strlen < 0) colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1(MAX)");
|
||||
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
|
||||
break;
|
||||
case DataType.PostgreSQL:
|
||||
case DataType.OdbcPostgreSQL:
|
||||
case DataType.OdbcKingbaseES:
|
||||
case DataType.ShenTong: //驱动引发的异常:“System.Data.OscarClient.OscarException”(位于 System.Data.OscarClient.dll 中)
|
||||
colattr.DbType = "BYTEA"; //变长二进制串
|
||||
break;
|
||||
case DataType.Oracle:
|
||||
colattr.DbType = "BLOB";
|
||||
break;
|
||||
case DataType.Dameng:
|
||||
colattr.DbType = "BLOB";
|
||||
break;
|
||||
case DataType.OdbcOracle:
|
||||
case DataType.OdbcDameng:
|
||||
colattr.DbType = "BLOB";
|
||||
break;
|
||||
case DataType.Sqlite:
|
||||
colattr.DbType = "BLOB";
|
||||
break;
|
||||
case DataType.MsAccess:
|
||||
if (strlen < 0) colattr.DbType = "BLOB";
|
||||
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (trytb.Columns.ContainsKey(colattr.Name)) throw new Exception($"ColumnAttribute.Name {colattr.Name} 重复存在,请检查(注意:不区分大小写)");
|
||||
if (trytb.ColumnsByCs.ContainsKey(p.Name)) throw new Exception($"属性名 {p.Name} 重复存在,请检查(注意:不区分大小写)");
|
||||
|
Reference in New Issue
Block a user