- 增加 MaxLength 特性的解析,实体字符串长度设置;

This commit is contained in:
28810 2019-09-11 20:40:52 +08:00
parent 96bf97bb7f
commit bddcf9c0bc
5 changed files with 56 additions and 11 deletions

View File

@ -1,6 +1,7 @@
using FreeSql; using FreeSql;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public class UserGroup : BaseEntity<UserGroup, int> public class UserGroup : BaseEntity<UserGroup, int>
{ {
@ -35,20 +36,24 @@ public class User1 : BaseEntity<User1, Guid>
/// <summary> /// <summary>
/// 登陆名 /// 登陆名
/// </summary> /// </summary>
[MaxLength(32)]
public string Username { get; set; } public string Username { get; set; }
/// <summary> /// <summary>
/// 昵称 /// 昵称
/// </summary> /// </summary>
[MaxLength(64)]
public string Nickname { get; set; } public string Nickname { get; set; }
/// <summary> /// <summary>
/// 头像 /// 头像
/// </summary> /// </summary>
[MaxLength(1024)]
public string Avatar { get; set; } public string Avatar { get; set; }
/// <summary> /// <summary>
/// 描述 /// 描述
/// </summary> /// </summary>
[MaxLength(4000)]
public string Description { get; set; } public string Description { get; set; }
} }

View File

@ -54,7 +54,7 @@ namespace orm_vs
{ {
testlist2.AddRange(list); testlist2.AddRange(list);
}); });
fsql.CodeFirst.SyncStructure(typeof(Song), typeof(Song_tag), typeof(Tag)); fsql.CodeFirst.SyncStructure(typeof(Song), typeof(Song_tag), typeof(Tag));
//sugar.CodeFirst.InitTables(typeof(Song), typeof(Song_tag), typeof(Tag)); //sugar.CodeFirst.InitTables(typeof(Song), typeof(Song_tag), typeof(Tag));
//sugar创建表失败SqlSugar.SqlSugarException: Sequence contains no elements //sugar创建表失败SqlSugar.SqlSugarException: Sequence contains no elements
@ -76,7 +76,7 @@ namespace orm_vs
Insert(sb, 1000, 10); Insert(sb, 1000, 10);
Console.Write(sb.ToString()); Console.Write(sb.ToString());
sb.Clear(); sb.Clear();
Insert(sb, 1, 1000); Insert(sb, 1, 1000);
Console.Write(sb.ToString()); Console.Write(sb.ToString());
sb.Clear(); sb.Clear();

View File

@ -15,7 +15,8 @@ namespace FreeSql.DataAnnotations
/// </summary> /// </summary>
public string OldName { get; set; } public string OldName { get; set; }
/// <summary> /// <summary>
/// 数据库类型,如: varchar(255) /// 数据库类型,如: varchar(255) <para></para>
/// 字符串长度,可使用特性 MaxLength(255)
/// </summary> /// </summary>
public string DbType { get; set; } public string DbType { get; set; }
@ -82,7 +83,7 @@ namespace FreeSql.DataAnnotations
internal short? _Position; internal short? _Position;
/// <summary> /// <summary>
/// 创建表时字段位置,规则如下: /// 创建表时字段位置(场景:实体继承后设置字段顺序),规则如下:
/// <para></para> /// <para></para>
/// &gt;0时排前面1,2,3... /// &gt;0时排前面1,2,3...
/// <para></para> /// <para></para>
@ -91,5 +92,5 @@ namespace FreeSql.DataAnnotations
/// &lt;0时排后面...-3,-2,-1 /// &lt;0时排后面...-3,-2,-1
/// </summary> /// </summary>
public short Position { get => _Position ?? 0; set => _Position = value; } public short Position { get => _Position ?? 0; set => _Position = value; }
} }
} }

View File

@ -61,7 +61,7 @@
</member> </member>
<member name="P:FreeSql.DataAnnotations.ColumnAttribute.Position"> <member name="P:FreeSql.DataAnnotations.ColumnAttribute.Position">
<summary> <summary>
创建表时字段位置,规则如下: 创建表时字段位置(场景:实体继承后设置字段顺序),规则如下:
<para></para> <para></para>
&gt;0时排前面1,2,3... &gt;0时排前面1,2,3...
<para></para> <para></para>

View File

@ -181,31 +181,31 @@ namespace FreeSql
switch (_entityPropertyConvertType) switch (_entityPropertyConvertType)
{ {
case StringConvertType.Lower: case StringConvertType.Lower:
ret.Aop.ConfigEntityProperty = (s, e) => ret.Aop.ConfigEntityProperty += (s, e) =>
{ {
e.ModifyResult.Name = e.Property.Name.ToLower(); e.ModifyResult.Name = e.Property.Name.ToLower();
}; };
break; break;
case StringConvertType.Upper: case StringConvertType.Upper:
ret.Aop.ConfigEntityProperty = (s, e) => ret.Aop.ConfigEntityProperty += (s, e) =>
{ {
e.ModifyResult.Name = e.Property.Name.ToUpper(); e.ModifyResult.Name = e.Property.Name.ToUpper();
}; };
break; break;
case StringConvertType.PascalCaseToUnderscore: case StringConvertType.PascalCaseToUnderscore:
ret.Aop.ConfigEntityProperty = (s, e) => ret.Aop.ConfigEntityProperty += (s, e) =>
{ {
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name); e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name);
}; };
break; break;
case StringConvertType.PascalCaseToUnderscoreWithLower: case StringConvertType.PascalCaseToUnderscoreWithLower:
ret.Aop.ConfigEntityProperty = (s, e) => ret.Aop.ConfigEntityProperty += (s, e) =>
{ {
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name).ToLower(); e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name).ToLower();
}; };
break; break;
case StringConvertType.PascalCaseToUnderscoreWithUpper: case StringConvertType.PascalCaseToUnderscoreWithUpper:
ret.Aop.ConfigEntityProperty = (s, e) => ret.Aop.ConfigEntityProperty += (s, e) =>
{ {
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name).ToUpper(); e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name).ToUpper();
}; };
@ -214,6 +214,45 @@ namespace FreeSql
break; break;
} }
} }
//处理 MaxLength
ret.Aop.ConfigEntityProperty += new EventHandler<Aop.ConfigEntityPropertyEventArgs>((s, e) =>
{
object[] attrs = null;
try
{
attrs = e.Property.GetCustomAttributes(false).ToArray(); //.net core 反射存在版本冲突问题,导致该方法异常
}
catch { }
var maxlenAttr = attrs.Where(a => {
return ((a as Attribute)?.TypeId as Type)?.Name == "MaxLengthAttribute";
}).FirstOrDefault();
if (maxlenAttr != null)
{
var lenProp = maxlenAttr.GetType().GetProperties().Where(a => a.PropertyType.IsNumberType()).FirstOrDefault();
if (lenProp != null && int.TryParse(string.Concat(lenProp.GetValue(maxlenAttr, null)), out var tryval) && tryval > 0)
{
switch (ret.Ado.DataType)
{
case DataType.MySql:
e.ModifyResult.DbType = $"varchar({tryval})";
break;
case DataType.SqlServer:
e.ModifyResult.DbType = $"nvarchar({tryval})";
break;
case DataType.PostgreSQL:
e.ModifyResult.DbType = $"varchar({tryval})";
break;
case DataType.Oracle:
e.ModifyResult.DbType = $"nvarchar2({tryval})";
break;
case DataType.Sqlite:
e.ModifyResult.DbType = $"nvarchar({tryval})";
break;
}
}
}
});
} }
return ret; return ret;