SqlSugar 实体特性 API 可否用.NET自带的 System.Data.Linq.Mapping 返回

C#论坛 老数据
9 3331

SqlSugar实体特性 API 有些污染实体类代码,可否用.NET自带的 System.Data.Linq.Mapping 命名空间下的特性代替?

热忱回答9

  • 支持定义特性 http://www.donet5.com/Doc/8/1141  看第四条

    0 回复
  • 汤汤 汤汤 VIP0
    2018/10/22

    @fate stay night:我的意思是仍然用标注,但是用 System.Data.Linq.Mapping 命名空间里的 TableAttribute, ColumnAttribute,而不用 SugarTable,SugarColumn,这样不用产生对SqlSugar的依赖。

    0 回复
  • 支持的,看第四条 

    0 回复
  • if(property.GetCustomAttributes存在你自定主键){

        column.IsPrimarykey=true;

    }

    0 回复
  • 依次论堆

    0 回复
  • 汤汤 汤汤 VIP0
    2018/10/29

    @fate stay night

    我的意思是SqlSugar内部就直接使用.Net framework里的 System.Data.Linq.Mapping,不用编写额外的映射表名和列名的代码,个人认为对SqlSugar的推广使用很有益处。

    或者可以使用配置文件,将开发者定义的Attribute类映射到SqlSugar的Attribute类,类似如下:

    System.Data.Linq.Mapping.TableAttribute => SqlSugar.SugarTable


    0 回复
  • 看来你还是不理论我的意思。

    我就写个用EF设置主键的例子

       public class ExtEntity: DemoBase
        {
            public static void Init()
            {
                SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() {
                    ConnectionString = Config.ConnectionString,
                    DbType = DbType.SqlServer,
                    IsAutoCloseConnection = true,
                     ConfigureExternalServices=new ConfigureExternalServices() {
                         EntityService = (property, column) => {
                             var attributes = property.GetCustomAttributes(true);
                             if (attributes.Any(it => it is KeyAttribute)) {
                                 column.IsPrimarykey = true;
                             }
                             
                         }
                     }
                });
         
                var sql=db.Queryable<StudentTest>().ToSql();
                var sql2 = db.Insertable<StudentTest>(new StudentTest()).ToSql();
            }
    
        }
    
        public class StudentTest {
            [Key]
            public string Id { get; set; }
            public string xxx { get; set; }
            public string Name { get; set; }
        }


    0 回复
  • SqlSugar存的意义是有一套自已的东西,并且是去迁就别人的东西,我只扩供扩展,支持你的用法

    0 回复
  • 汤汤 汤汤 VIP0
    2018/10/29

    @fate stay night:明白,谢谢!另外表名有提供这种方法吗?还是只有 db.MappingTables.Add("实体类名称", "数据库表名") 这种方式?

    0 回复