精品熟女碰碰人人a久久,多姿,欧美欧美a v日韩中文字幕,日本福利片秋霞国产午夜,欧美成人禁片在线观看

.NET之生成數據庫全流程實現

.net之生成數據庫全流程實現

 

開篇語

本文主要是回顧下從項目創建到生成數據到數據庫(代碼優先)的全部過程。采用efcore作為orm框架。

本次示例環境:vs2019、net5、mysql

 

創建項目

本次事例代碼是用過vs2019創建的asp.net core web api項目

可以通過可視化界面創建或者通過命令行創建

dotnet new webapi -o net5bydocker

 

創建實體類

安裝組件

  <packagereference include="pomelo.entityframeworkcore.mysql" version="5.0.0" />
  <packagereference include="pomelo.entityframeworkcore.mysql.json.newtonsoft" version="5.0.0" />

增加實體類

  [table("user")]
  public class user
  {
      public user()
      {
          id = guid.newguid().tostring();
      }

      public user(string account, string password, string creater) : this()
      {
          account = account;
          password = password;
          deleted = false;
          setcreater(creater);
      }

      [key]
      [comment("主鍵")]
      [stringlength(36)]
      [required]
      public string id { get; private set; }

      [comment("帳號")]
      [stringlength(36)]
      [required]
      public string account { get; private set; }

      [comment("密碼")]
      [stringlength(36)]
      [required]
      public string password { get; private set; }

      [comment("余額")]
      [column(typename = "decimal(18, 2)")]
      [required]
      public decimal money { get; set; }

      [comment("是否刪除")]
      [column(typename = "tinyint(1)")]
      [required]
      public bool deleted { get; private set; }

      [comment("創建人")]
      [stringlength(20)]
      [required]
      public string creater { get; private set; }

      [comment("創建時間")]
      [required]
      public datetime createtime { get; private set; }

      [comment("修改人")]
      [stringlength(20)]
      [required]
      public string modifyer { get; private set; }

      [comment("修改時間")]
      [required]
      public datetime modifytime { get; private set; }

      public void setcreater(string name)
      {
          creater = name;
          createtime = datetime.now;
          setmodifyer(name);
      }

      public void setmodifyer(string name)
      {
          modifyer = name;
          modifytime = datetime.now;
      }
  }

這種只是增加實體類類型的一種方式,可能這種看著比較亂,還可以通過onmodelcreating實現,詳情看參考文檔

增加數據庫上下文opendbcontext

  public class opendbcontext : dbcontext
  {
      public opendbcontext(dbcontextoptions<opendbcontext> options)
          : base(options)
      {
      }

      public dbset<user> users { get; set; }
  }

startup注入連接數據庫操作

          var connection = configuration["dbconfig:mysql:connectionstring"];
          var migrationsassembly = introspectionextensions.gettypeinfo(typeof(startup)).assembly.getname().name;
          services.adddbcontext<opendbcontext>(option => option.usemysql(connection, serverversion.autodetect(connection), x =>
          {
              x.usenewtonsoftjson();
              x.migrationsassembly(migrationsassembly);
          }));

 

生成遷移文件

引用組件

<packagereference include="microsoft.entityframeworkcore.design" version="5.0.5">
<packagereference include="microsoft.entityframeworkcore.tools" version="5.0.5">

遷移命令

add-migration init

結果

要看下生成的遷移文件是否是自己預期的那樣子,也可以在這一步就生成數據庫,命令:update-database

 

數據種子

增加opendbsend類,添加數據種子

  public class opendbsend
  {
      /// <summary>
      /// 生成數據庫以及數據種子
      /// </summary>
      /// <param name="dbcontext">數據庫上下文</param>
      /// <param name="loggerfactory">日志</param>
      /// <param name="retry">重試次數</param>
      /// <returns></returns>
      public static async task seedasync(opendbcontext dbcontext,
          iloggerfactory loggerfactory,
          int? retry = 0)
      {
          int retryforavailability = retry.value;
          try
          {
              dbcontext.database.migrate();//如果當前數據庫不存在按照當前 model 創建,如果存在則將數據庫調整到和當前 model 匹配
              await initializeasync(dbcontext).configureawait(false);

              //if (dbcontext.database.ensurecreated())//如果當前數據庫不存在按照當前 model創建,如果存在則不管了。
              //  await initializeasync(dbcontext).configureawait(false);
          }
          catch (exception ex)
          {
              if (retryforavailability < 3)
              {
                  retryforavailability++;
                  var log = loggerfactory.createlogger<opendbsend>();
                  log.logerror(ex.message);
                  await seedasync(dbcontext, loggerfactory, retryforavailability).configureawait(false);
              }
          }
      }

      /// <summary>
      /// 初始化數據
      /// </summary>
      /// <param name="context"></param>
      /// <returns></returns>
      public static async task initializeasync(opendbcontext context)
      {
          if (!context.set<user>().any())
          {
              await context.set<user>().addasync(new user("azrng", "123456", "azrng")).configureawait(false);
              await context.set<user>().addasync(new user("張三", "123456", "azrng")).configureawait(false);
          }
          await context.savechangesasync().configureawait(false);
      }
  }

設置項目啟動時候調用

      public static async task main(string[] args)
      {
          var host = createhostbuilder(args).build();
          using (var scope = host.services.createscope())
          {
              var services = scope.serviceprovider;
              var loggerfactory = services.getrequiredservice<iloggerfactory>();
              var _logger = loggerfactory.createlogger<program>();
              try
              {
                  var opencontext = services.getrequiredservice<opendbcontext>();
                  await opendbsend.seedasync(opencontext, loggerfactory).configureawait(false);
              }
              catch (exception ex)
              {
                  _logger.logerror(ex, $"項目啟動出錯  {ex.message}");
              }
          }

          await host.runasync().configureawait(false);
      }

 

生成數據庫

啟動項目,自動生成數據庫

表結構如下

如果后期數據庫字段或者結構有變動,可以再次生成遷移文件然后生成數據庫

 

查詢數據

  /// <summary>
  /// 用戶接口
  /// </summary>
  public interface iuserservice
  {
      string getname();

      /// <summary>
      /// 查詢用戶信息
      /// </summary>
      /// <param name="account"></param>
      /// <returns></returns>
      task<user> getdetailsasync(string account);
  }

  /// <summary>
  /// 用戶實現
  /// </summary>
  public class userservice : iuserservice
  {
      private readonly opendbcontext _dbcontext;

      public userservice(opendbcontext dbcontext)
      {
          _dbcontext = dbcontext;
      }

      public string getname()
      {
          return "azrng";
      }

      ///<inheritdoc cref="iuserservice.getdetailsasync(string)"/>
      public async task<user> getdetailsasync(string account)
      {
          return await _dbcontext.set<user>().firstordefaultasync(t => t.account == account).configureawait(false);
      }
  }

一般更推薦建立指定的返回model類,然后只查詢需要的內容,不直接返回實體類

控制器方法

      /// <summary>
      /// 查詢用戶詳情
      /// </summary>
      /// <param name="account"></param>
      /// <returns></returns>
      [httpget]
      public async task<actionresult<user>> getdetailsasync(string account)
      {
          return await _userservice.getdetailsasync(account).configureawait(false);
      }

查詢結果

{
"id": "e8976d0a-6ee9-4e2e-b8d8-1fe6e85b727b",
"account": "azrng",
"password": "123456",
"money": 0,
"deleted": false,
"creater": "azrng",
"createtime": "2021-05-09t15:48:45.730302",
"modifyer": "azrng",
"modifytime": "2021-05-09t15:48:45.730425"
}

 

參考文檔

實體類型:https://docs.microsoft.com/zh-cn/ef/core/modeling/entity-types?tabs=data-annotations

實體屬性:https://docs.microsoft.com/zh-cn/ef/core/modeling/entity-properties?tabs=data-annotations%2cwithout-nrt

關于.net之生成數據庫全流程實現的文章就介紹至此,更多相關.net 生成數據庫內容請搜索碩編程以前的文章,希望大家多多支持碩編程

下一節:.net集成敏感詞組件的步驟

asp.net編程技術

相關文章