Skip to content
FreeSql 官方文档FreeSql 官方文档
指南
  • 指南

      • code-first
        • 实体特性✨
          • 流式接口
            • 自定义特性
              • 优先级
              • 类型映射
                • 导航属性✨
                • DB First
                  • 表达式函数
                    • 事务
                      • 过滤器
                        • ADO
                          • AOP✨
                            • 读写分离
                              • 分表分库
                                • 多租户
                                  • 性能
                                    • 你不知道的功能✨

                                    自定义特性

                                    2021年2月5日小于 1 分钟约 153 字

                                    此页内容
                                    • 优先级

                                    # 自定义特性

                                    本功能可实现与其他 ORM 使用一套 Attribute,避免维护两份实体特性的烦恼:

                                    v1.4.0+ 已自动识别 EFCore 实体特性 Key/Required/NotMapped/MaxLength/StringLength/DatabaseGenerated/Table/Column

                                    fsql.Aop.ConfigEntity += (s, e) => {
                                      var attr = e.EntityType.GetCustomAttributes(typeof(MyTableAttribute), false).FirstOrDefault() as MyTableAttribute;
                                      if (attr != null)
                                        e.ModifyResult.Name = attr.Name; //表名
                                    };
                                    fsql.Aop.ConfigEntityProperty += (s, e) => {
                                      var attr = e.Property.GetCustomAttributes(typeof(MyColumnAttribute), false).FirstOrDefault() as MyColumnAttribute;
                                      if (attr != null)
                                        e.ModifyResult.Name = attr.Name; //字段名
                                    };
                                    
                                    [MyTable("xxx")]
                                    class YourEntity {
                                      [MyColumn("id")]
                                      public int pkid { get; set; }
                                    }
                                    
                                    class MyTableAttribute : Attribute {
                                      public string Name { get; }
                                      public MyTableAttribute(string name)
                                      {
                                        this.Name = name;
                                      }
                                    }
                                    class MyColumnAttribute : Attribute {
                                      public string Name { get; }
                                      public MyColumnAttribute(string name)
                                      {
                                        this.Name = name;
                                      }
                                    }
                                    

                                    # 优先级

                                    数据库特性 > 实体特性 > FluentApi(配置特性) > AOP(配置特性)

                                    在 GitHub 上编辑此页open in new window
                                    上次编辑于: 2021/6/17 上午12:56:09
                                    贡献者: luoyunchong
                                    上一页
                                    流式接口
                                    下一页
                                    类型映射
                                    Copyright © 2018-present nicye