Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aop.ConfigEntityProperty如何自定义属性指定字段长度 #69

Closed
systemhejiyong opened this issue Jul 6, 2019 · 10 comments
Closed

Comments

@systemhejiyong
Copy link
Contributor

systemhejiyong commented Jul 6, 2019

Aop.ConfigEntityProperty代码:

freesql.Aop.ConfigEntityProperty = (s, e) =>
{
	if (e.Property.PropertyType == typeof(string))
	{
		var strLen = e.Property.GetCustomAttribute<System.ComponentModel.DataAnnotations.MaxLengthAttribute>();
		if (strLen != null && e.ModifyResult.DbType.IndexOf("(") == 0)
		{
			e.ModifyResult.DbType = e.ModifyResult.DbType + "(" + strLen.Length + ")";
		}
	}
};

实体属性:

[MaxLength(50)]
 public string UserName { get; set; }

代码中e.ModifyResult.DbType将会等于空,最终生成数据表就报语法错误了。
image

生成的脚本中有两个问题一个是无类型,一个是表名多出了一个_FreeSqlTmp_
image

@2881099
Copy link
Collaborator

2881099 commented Jul 7, 2019

类型此时为空,所以都变成了(数字)

FreeSqlTmp 是正常的,结构变化后,创建临时表,导入数据后,删除旧表,然后再把他的名改回旧表的名字

@2881099
Copy link
Collaborator

2881099 commented Jul 7, 2019

freesql.Aop.ConfigEntityProperty = (s, e) =>
{
	if (e.Property.PropertyType == typeof(string))
	{
		var strLen = e.Property.GetCustomAttribute<System.ComponentModel.DataAnnotations.MaxLengthAttribute>();
		if (strLen != null)
		{
			e.ModifyResult.DbType = freesql.CodeFirst.GetDbInfo(e.Property.PropertyType)?.dbtype + "(" + strLen.Length + ")";
		}
	}
};

改成这样试试

@systemhejiyong
Copy link
Contributor Author

systemhejiyong commented Jul 7, 2019

这样可以,现在不知道为什么有一个类的一直没有设置长度成功。
image

image

Git地址: https://gitee.com/HoganCode/FreeSqlCms

@2881099
Copy link
Collaborator

2881099 commented Jul 8, 2019

断点看了没

@2881099
Copy link
Collaborator

2881099 commented Jul 8, 2019

把断点打在 Aop.ConfigEntityProperty 里面调一下看看,是不是特性名不对,用了其他空间的?

@2881099
Copy link
Collaborator

2881099 commented Jul 8, 2019

又是不是可能其他地方配置了 FluentAPI,有优先级

优先级
数据库特性 > 实体特性 > FluantApi(配置特性) > Aop(配置特性)

@systemhejiyong
Copy link
Contributor Author

已经可以了, 忘记当用到SysRole这个实体的时候,才会去执行修改表。

@pjy612
Copy link

pjy612 commented Nov 7, 2019

囧 为啥不直接 在 ModifyResult 里面 加个 int? Length 的属性?

@2881099
Copy link
Collaborator

2881099 commented Nov 7, 2019

v0.9.12

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

class Topic {
    [MaxLength(128)]
    public string Title { get; set; }
}

MaxLength 可以是任何命令空间下的特性

@pjy612
Copy link

pjy612 commented Nov 7, 2019

e.ModifyResult.DbType = freesql.CodeFirst.GetDbInfo(e.Property.PropertyType)?.dbtype + "(" + strLen.Length + ")";

这段 要是能整合到 一个 属性里面 是不是更好 避免出错? 在Aop处理也直观一些。
一些第三方或老项目的特性 直接在 Aop里面动刀就好了
目前 上述代码能实现功能 不过 没那么直观 _(:з」∠)_

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants