<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C# 動態(tài)創(chuàng)建類,動態(tài)創(chuàng)建表,支持多庫的數(shù)據(jù)庫維護方案

          共 16922字,需瀏覽 34分鐘

           ·

          2023-06-21 21:52


          一、創(chuàng)建表

          SqlSugar支持了3種模式的建表(無實體建表、實體建表,實體特性建表),非常的靈活

          可以多個數(shù)據(jù)庫 MYSQL MSSQL ORACLE SQLITE PGSQL 等用同一語法創(chuàng)建數(shù)據(jù)庫,不需要考慮數(shù)據(jù)庫的兼容性

          中間標準:

          string 大文本5.1.3.44-preview06 推薦[SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)]
          string 設(shè)置長度的字符串[SugarColumn(Length=10)] public string FieldName{ get; set; }
          int 整數(shù)public int FieldName{ get; set; }
          short 整數(shù)小public short FieldName{ get; set; }
          long 大數(shù)字public long FieldName{ get; set; }
          bool 真假public bool FieldName{ get; set; }
          decimal 默認public decimal FieldName{ get; set; }
          decimal 自定義//18,2 18,4 18,6 這幾種兼容性好[SugarColumn(Length=18,DecimalDigits=2)]public decimal FieldName{ get; set; }
          DateTime 時間public DateTime FieldName{ get; set; }
          枚舉 (數(shù)據(jù)庫存int)public 枚舉 FieldName{ get; set; }
          byte[] 二進制public byte[] FileInfo{get;set;}建議:升級到 SqlSugarCore 5.1.3.46-preview09 及以上對多庫支持了比較好
          SqlServer特殊配置:和他庫不同一般選用Nvarchar,可以使用這個配置讓他和其他數(shù)據(jù)庫區(qū)分(其他庫是varchar) DbType = SqlSugar.DbType.SqlServer,`` ``ConnectionString ="字符串",`` ``IsAutoCloseConnection = ``true``,`` ``MoreSettings=``new ConnMoreSettings() {`` `` ``SqlServerCodeFirstNvarchar= ``true``,`` `` ``}

          1.1、通過特性建表

          我們可以通過創(chuàng)建實體來進行建表

          public class CodeFirstTable1
          {
                  [SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
                  public int Id { getset; }
                  public string Name { getset; }
                  //ColumnDataType 自定格式的情況 length不要設(shè)置 (想要多庫兼容看4.2和9)
                  [SugarColumn(ColumnDataType = "Nvarchar(255)")]
                  public string Text { getset; }
                  [SugarColumn(IsNullable = true)]//可以為NULL
                  public DateTime CreateTime { getset; }
          }
            
          /***創(chuàng)建單個表***/
          db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(CodeFirstTable1));//這樣一個表就能成功創(chuàng)建了
          /***手動建多個表***/
          db.CodeFirst.SetStringDefaultLength(200)
          .InitTables(typeof(CodeFirstTable1),typeof(CodeFirstTable2));

          建表特性如下

          名稱描述
          IsIdentity是否創(chuàng)建自增標識
          IsPrimaryKey是否創(chuàng)建主鍵標識
          ColumnName創(chuàng)建數(shù)據(jù)庫字段的名稱(默認取實體類屬性名稱)
          ColumnDataType創(chuàng)建數(shù)據(jù)庫字段的類型用法1:“varchar(20)” 不需要設(shè)置長度用法2:   不設(shè)置該參數(shù)  系統(tǒng)會根據(jù)C#類型自動生成相應(yīng)的數(shù)據(jù)庫類型 用法3:    多庫兼容可以用 :看標題9
          IsIgnoreORM不處理該列
          ColumnDescription備注 表注釋 (新版本支持XML文件)
          Length長度 設(shè)成10會生成  xxx類型(10), 沒括號的不設(shè)置
          IsNullable是否可以為null默為false
          DecimalDigits精度 如 decimal(18,2) length=18,DecimalDigits=2
          OracleSequenceName設(shè)置Oracle序列,設(shè)置后該列等同于自增列
          OldColumnName修改列名用,這樣不會新增或者刪除列
          IndexGroupNameList已棄用 ,新用法看文檔4.3
          UniqueGroupNameList已棄用, 新用法看文檔4.3
          注意:有2個屬性用處不同DefaultValue IsOnlyIgnoreInsertDefaultValue=默認值 用來建表設(shè)置字段默認值IsOnlyIgnoreInsert=true 插入數(shù)據(jù)時取默認值很多情況需要2個一起使用如果只建表不插入數(shù)據(jù)用1個 如果建表并且插入數(shù)據(jù)用2個

          2.2、無特性建表

          如果我們的實體不需要加特性,那么我們可以通過特性方式建表

          SugarClient db = new SqlSugarClient(new ConnectionConfig()
          {
              DbType = DbType.SqlServer,
              ConnectionString = Config.ConnectionString3,
              InitKeyType = InitKeyType.Attribute,
              IsAutoCloseConnection = true,
              ConfigureExternalServices = new ConfigureExternalServices()
              {
                  EntityService = (s, p) =>
                  {
                      //如果是Order實體進行相關(guān)配置
                      p.IfTable<Order>()
                      .UpdateProperty(it => it.id, it =>
                      {
                          it.IsIdentity = true;
                          it.IsPrimarykey = true;
                      })
                      .UpdateProperty(it => it.Name, it => {
                          it.Length = 100;
                          it.IsNullable = true;
             
                      })
                      .OneToOne(it => it.Item, nameof(Order.ItemId));
                         
                      //如果Custom實體進行相關(guān)配置
                       p.IfTable<Custom>()
                       .UpdateProperty(it => it.id, it =>
                       {
                          it.IsIdentity = true;
                          it.IsPrimarykey = true;
                       })
                        .UpdateProperty(it => it.Text, it => {
                          it.DataType= StaticConfig.CodeFirst_BigString;//支持多庫的MaxString用法
                        })
                         
                                     
                      //可以結(jié)合全局邏輯一起使用,下面的和上面的有沖突的話,下面會覆蓋上面的
                          
                         
                      //統(tǒng)一設(shè)置 nullable等于isnullable=true
                      //低版本C#看標題2.2
                       if(p.IsPrimaryKey==false&&new NullabilityInfoContext()
                                   .Create(c).WriteState is NullabilityState.Nullable)
                       {
                                     p.IsNullable = true;
                       }
                         
                          
                          
                  }
              }
          });
          //性能說明:
          //EntityService 相同實體只會執(zhí)行一次性不需太操作

          1.3、無實體建表

          功能與實體建類一模一樣,如果使用SqlSugar中間標準可以支持多個數(shù)據(jù)庫一套代碼建表

          var type = db.DynamicBuilder().CreateClass("UnitEntityA",
                         new SugarTable()
                         {
                             TableDescription = "表備注",
                             //DisabledUpdateAll=true 可以禁止更新只創(chuàng)建
                         }
                     )
                    .CreateProperty("Id"typeof(int), new SugarColumn() { IsPrimaryKey = true, IsIdentity = true, ColumnDescription = "列備注" })
                    .CreateProperty("Name"typeof(string), new SugarColumn() {Length=200, ColumnDescription = "列備注" })
                    .BuilderType();
           
                     db.CodeFirst.InitTables(type);

          三、數(shù)據(jù)庫維護

          SqlSugar有一套數(shù)據(jù)庫維護API,并且能夠很好的支持多種數(shù)據(jù)庫,例如備份數(shù)據(jù)庫等常用功能

          //例1 獲取所有表
          var tables = db.DbMaintenance.GetTableInfoList(false);//true 走緩存 false不走緩存
          foreach (var table in tables)
          {
               Console.WriteLine(table.Description);//輸出表信息
                 
               //獲取列信息
               //var columns=db.DbMaintenance.GetColumnInfosByTableName("表名",false);
          }
            
          //例2
          db.DbMaintenance.IsAnyTable("tablename",false)//驗證表名是否緩存不走緩存

          所以API

          GetDataBaseList獲取所有數(shù)據(jù)庫名稱List
          GetViewInfoList查詢所有視圖List
          GetTableInfoList獲取所有表,查詢所有的表 (GetTableInfoList(是否緩存))List
          GetColumnInfosByTableName獲取列根據(jù)表名,獲取字段,字段信息GetColumnInfosByTableName(表名,是否緩存)List
          GetIsIdentities獲取自增列List
          GetPrimaries獲取主鍵List
          IsAnyTable表是否存在,判斷表存不存在 ( IsAny(表名,是否緩存))bool
          IsAnyColumn列是否存在bool
          IsPrimaryKey主鍵是否存在bool
          IsIdentity自增是否存在bool
          IsAnyConstraint約束是否存在bool
          DropTable刪除表bool
          TruncateTable清空表bool
          CreateTable看標題 1.1,1.2,1.3bool
          AddColumn添加列bool
          UpdateColumn更新列bool
          AddPrimaryKey添加主鍵bool
          DropConstraint刪除約束bool
          BackupDataBase備份庫bool
          DropColumn刪除列bool
          RenameColumn重命名列bool
          AddDefaultValue添加默認值bool
          AddTableRemark添加表描述,表注釋bool
          AddColumnRemark添加列描述,表注釋bool
          DeleteColumnRemark刪除列描述,表注釋bool
          RenameTable重命名表bool
          CreateIndex創(chuàng)建索引,唯一約束(唯一索引)bool
          IsAnyIndex索引是否存在bool
          GetIndexList獲取所有索引
          GetProcList獲取所有存儲過程

          四、跨庫支持

          可以自動識別在哪個庫

          實體

          [TenantAttribute("1")]//對應(yīng)ConfigId
          public class C1Table
          {
             public string Id { getset; }
          }
             
          [TenantAttribute("2")]
          public class C2Table
          {
              public string Id { getset; }
          }

          查詢

          //通過ConfigId進行數(shù)據(jù)庫區(qū)分
          var db = new SqlSugarClient(new List<ConnectionConfig>()
          {
           //這兒聲名所有上下文都生效
           new ConnectionConfig(){ConfigId="0",DbType=DbType.SqlServer,ConnectionString=..,IsAutoCloseConnection=true},
           new ConnectionConfig(){ConfigId="1",DbType=DbType.MySql,ConnectionString=..,IsAutoCloseConnection=true  }
          });
          //自動跨庫聯(lián)表查詢
          var query5 = db.QueryableWithAttr<Order>()//如果MySql和SqlServer自動支持同服務(wù)器的跨庫聯(lián)表查詢
                   .LeftJoin<Custom>   ((o, cus ) => o.CustomId == cus.Id)//多個條件用&&
                   .LeftJoin<OrderDetail> ((o, cus, oritem) => o.Id == oritem.OrderId)
                   .Where(o => o.Id == 1
                   .Select((o, cus , oritem) => new ViewOrder { Id = o.Id, CustomName = cus.Name })
                   .ToList(); 
          //手動跨庫聯(lián)表查詢 ,這種方式結(jié)合dblink可以跨服務(wù)器
          var query5 = db.Queryable().As("xxx.表名")
                   .LeftJoin<Custom>   ((o, cus ) => o.CustomId == cus.Id ,"yyyy.表名")
                   .LeftJoin<OrderDetail> ((o, cus, oritem) => o.Id == oritem.OrderId ,"zzzz.名表")
                   .Where(o => o.Id == 1
                   .Select((o, cus , oritem) => new ViewOrder { Id = o.Id, CustomName = cus.Name })
                   .ToList();

          插入

          db.InsertableWithAttr(list).Execommand()

          更新

          db.UpdateableWithAttr(list).Execommand()

          刪除

          db.UpdateableWithAttr(list).Execommand() 

          只要實體配置了數(shù)據(jù)庫,就不要考慮換庫了,直接使用,并且支持事務(wù)

          四、過濾器

          SqlSugar支持了全新的過濾器,可以是接口,集成該接口的類都生效,支持多表查詢

          db.QueryFilter
          .AddTableFilter<IDeletedFilter>(it => it.IsDeleted==false)//IDeletedFilter是自定義接口,繼承這個接口的實體有效
          .AddTableFilterIF<ITenantFilter>(isAdmint==false,it=>it.OrgId==用戶OrgId);//ITenantFilter自定義接口
            
          //用例1:單條語句清空,只影響當前語句
          db.Queryable<Order>().ClearFilter().ToList();//所有過濾器都無效
          db.Queryable<Order>().ClearFilter<IDeletedFilter>().ToList();//只有IDeletedFilter過濾器無效
          db.Queryable<Order>().ClearFilter<IDeletedFilter,ITenantFilter>().ToList();//IDeletedFilter+ITenantFilter無效
              
          //用例2:當前上下文清空 ,不會影響其他請求,只是當前請求清空
          db.QueryFilter.Clear();
          db.QueryFilter.Clear<IDeletedFilter>();
            
          //用例3:清空并還原 ,不會影響其他請求,只是當前請求清空
          db.QueryFilter.ClearAndBackup();//有多個重載 ClearAndBackup<T,T2>();
          db.Queryable<Order>().ToList();
          db.QueryFilter.Restore();//還原過濾器 (適合下面代碼還需要過濾器情況)

          五、子查詢升級

          1、ToList() 可以直接查詢一個對象

          2、First() 可以直接查單個對象

          3、ToList(it=>it.Id) 可以查List< int >一個字段集合

          4、SelectStringJoin(z => z.Name, ",") 子查詢將一列用逗號拼接成字符串

          var list=db.Queryable< Order >()
          .Select(it => new
                  {
                    CustomId = it.CustomId,
                    OrderId = it.Id,
                    OrderName = it.Name,
                    CustomList = SqlFunc.Subqueryable< Custom >().Where(c => c.Id == it.CustomId).ToList()
                  })
                 .ToList();

          六、自定義類型支持

          自定義類型轉(zhuǎn)換器

          下面只是講解怎么定義轉(zhuǎn)換器,ORM自帶的功能就包含下面功能,只是用來講解

          public class DictionaryConvert : ISugarDataConverter
          {
          public SugarParameter ParameterConverter<T>(object valueint i)
          {
          //該功能ORM自帶的IsJson就能實現(xiàn)這里只是用這個用例來給大家學習
          var name = "@myp" + i;
          var str = new SerializeService().SerializeObject(value);
          return new SugarParameter(name, str);
          }
           
          public T QueryConverter<T>(IDataRecord dr, int i)
          {
          //打斷點調(diào)試
          //該功能ORM自帶的IsJson就能實現(xiàn)這里只是用這個用例來給大家學習
          var str = dr.GetValue(i) + "";
          return new SerializeService().DeserializeObject<T>(str);
          }
          }
          //使用自定義轉(zhuǎn)換器
          [SugarColumn(ColumnDataType="varchar(2000)",SqlParameterDbType=typeof(DictionaryConvert))]
          public Dictionary<stringobject> DcValue { getset; }//5.1.3.53-preview08 

          現(xiàn)有類型支持

          json類型

          https://www.donet5.com/Home/Doc?typeId=1232

          枚舉類型

          int存儲:直接用就行了

          public DbType DcValue { getset; }

          string存儲:高版本如下寫法

          [SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))]
          public DbType DcValue { getset; }

          3、數(shù)據(jù)庫獨有類型支持

          看左邊菜單 【數(shù)據(jù)庫特性】 該菜單下面有 SqlServer菜單或者MySql菜單等,針對不同數(shù)據(jù)庫都有專門的介紹

          總結(jié)

          SqlSugar在2021年到2022年大量的開源應(yīng)用使用了SqlSugar,帶動了SqlSugar的快速發(fā)展,我這邊要感謝大家

          Admin.NET通用管理平臺

          ZrAdminNetCore 后臺

          管理Yi框架(Ruoyi Vue)

          SimpleAdmin (new)

          vboot-netmagic.net (Vue2.0)

          網(wǎng)關(guān)采集系統(tǒng)(Blazor)

          RuYiAdmin

          CoreShop商城

          Blog.Core

          YuebonCore

          企業(yè)級框架Furion

          WebFirst

          騰訊APIJSON.NET

          WaterCloud微服務(wù)

          ViperFamilyBucket應(yīng)用框架通用后臺

          SmartSqlWMS倉庫管理系統(tǒng)a

          pevolo-apiFytSoaCms

          開源項目

          https://www.donet5.com/Home/Doc?typeId=1215

          轉(zhuǎn)自:果糖大數(shù)據(jù)科技

          鏈接:cnblogs.com/sunkaixuan/p/17240546.html







          回復 【關(guān)閉】學永久關(guān)閉App開屏廣告
          回復 【刪除】學自動檢測那個微信好友刪除、拉黑
          回復 【福利】學查看微粒貸額度獲取20元微信紅包
          回復 【手冊】獲取3萬字.NET、C#工程師面試手冊
          回復 【幫助】獲取100+個常用的C#幫助類庫
          回復 【加群】加入DotNet學習交流群

          瀏覽 30
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  大鸡吧插入视频 | 日本大骚逼 | 视频啪啪网 | 免费精品久久久久久中文字幕-无删减 | 亚洲无码黄 |