Skip to content

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

Closed

Description

@systemhejiyong

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

Activity

  1. 2881099 commented on Jul 7, 2019

    @2881099
    Collaborator

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

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

  2. 2881099 commented on Jul 7, 2019

    @2881099
    Collaborator
    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 + ")";
    		}
    	}
    };
    

    改成这样试试

  3. systemhejiyong commented on Jul 7, 2019

    @systemhejiyong
    ContributorAuthor

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

    image

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

  4. 2881099 commented on Jul 8, 2019

    @2881099
    Collaborator

    断点看了没

  5. 2881099 commented on Jul 8, 2019

    @2881099
    Collaborator

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

  6. 2881099 commented on Jul 8, 2019

    @2881099
    Collaborator

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

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

  7. systemhejiyong commented on Jul 8, 2019

    @systemhejiyong
    ContributorAuthor

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

  8. pjy612 commented on Nov 7, 2019

    @pjy612

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

  9. 2881099 commented on Nov 7, 2019

    @2881099
    Collaborator

    v0.9.12

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

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

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

  10. pjy612 commented on Nov 7, 2019

    @pjy612
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions