mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
- 增加 MaxLength 特性的解析,实体字符串长度设置;
This commit is contained in:
parent
96bf97bb7f
commit
bddcf9c0bc
@ -1,6 +1,7 @@
|
||||
using FreeSql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
public class UserGroup : BaseEntity<UserGroup, int>
|
||||
{
|
||||
@ -35,20 +36,24 @@ public class User1 : BaseEntity<User1, Guid>
|
||||
/// <summary>
|
||||
/// 登陆名
|
||||
/// </summary>
|
||||
[MaxLength(32)]
|
||||
public string Username { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 昵称
|
||||
/// </summary>
|
||||
[MaxLength(64)]
|
||||
public string Nickname { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 头像
|
||||
/// </summary>
|
||||
[MaxLength(1024)]
|
||||
public string Avatar { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 描述
|
||||
/// </summary>
|
||||
[MaxLength(4000)]
|
||||
public string Description { get; set; }
|
||||
}
|
||||
|
@ -15,7 +15,8 @@ namespace FreeSql.DataAnnotations
|
||||
/// </summary>
|
||||
public string OldName { get; set; }
|
||||
/// <summary>
|
||||
/// 数据库类型,如: varchar(255)
|
||||
/// 数据库类型,如: varchar(255) <para></para>
|
||||
/// 字符串长度,可使用特性 MaxLength(255)
|
||||
/// </summary>
|
||||
public string DbType { get; set; }
|
||||
|
||||
@ -82,7 +83,7 @@ namespace FreeSql.DataAnnotations
|
||||
|
||||
internal short? _Position;
|
||||
/// <summary>
|
||||
/// 创建表时字段位置,规则如下:
|
||||
/// 创建表时字段的位置(场景:实体继承后设置字段顺序),规则如下:
|
||||
/// <para></para>
|
||||
/// >0时排前面,1,2,3...
|
||||
/// <para></para>
|
||||
@ -91,5 +92,5 @@ namespace FreeSql.DataAnnotations
|
||||
/// <0时排后面,...-3,-2,-1
|
||||
/// </summary>
|
||||
public short Position { get => _Position ?? 0; set => _Position = value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -61,7 +61,7 @@
|
||||
</member>
|
||||
<member name="P:FreeSql.DataAnnotations.ColumnAttribute.Position">
|
||||
<summary>
|
||||
创建表时字段位置,规则如下:
|
||||
创建表时字段的位置(场景:实体继承后设置字段顺序),规则如下:
|
||||
<para></para>
|
||||
>0时排前面,1,2,3...
|
||||
<para></para>
|
||||
|
@ -181,31 +181,31 @@ namespace FreeSql
|
||||
switch (_entityPropertyConvertType)
|
||||
{
|
||||
case StringConvertType.Lower:
|
||||
ret.Aop.ConfigEntityProperty = (s, e) =>
|
||||
ret.Aop.ConfigEntityProperty += (s, e) =>
|
||||
{
|
||||
e.ModifyResult.Name = e.Property.Name.ToLower();
|
||||
};
|
||||
break;
|
||||
case StringConvertType.Upper:
|
||||
ret.Aop.ConfigEntityProperty = (s, e) =>
|
||||
ret.Aop.ConfigEntityProperty += (s, e) =>
|
||||
{
|
||||
e.ModifyResult.Name = e.Property.Name.ToUpper();
|
||||
};
|
||||
break;
|
||||
case StringConvertType.PascalCaseToUnderscore:
|
||||
ret.Aop.ConfigEntityProperty = (s, e) =>
|
||||
ret.Aop.ConfigEntityProperty += (s, e) =>
|
||||
{
|
||||
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name);
|
||||
};
|
||||
break;
|
||||
case StringConvertType.PascalCaseToUnderscoreWithLower:
|
||||
ret.Aop.ConfigEntityProperty = (s, e) =>
|
||||
ret.Aop.ConfigEntityProperty += (s, e) =>
|
||||
{
|
||||
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name).ToLower();
|
||||
};
|
||||
break;
|
||||
case StringConvertType.PascalCaseToUnderscoreWithUpper:
|
||||
ret.Aop.ConfigEntityProperty = (s, e) =>
|
||||
ret.Aop.ConfigEntityProperty += (s, e) =>
|
||||
{
|
||||
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name).ToUpper();
|
||||
};
|
||||
@ -214,6 +214,45 @@ namespace FreeSql
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user