chiou0402
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # [112]天方科技 ASP.net core 教育訓練 1120314(IIS部署、SQL Server帳號設定步驟、EF Core Power Tools) ## IIS部署 ### 打開控制台→點擊程式集→點擊程式和功能→點擊開啟或關閉Windows功能 ![](https://i.imgur.com/q6Fh1GC.png) ### 找到IIS,並點擊安裝,使用預設功能即可 ![](https://i.imgur.com/yekTDM5.png) ### 開啟IIS管理員 ![](https://i.imgur.com/AeMH7NR.png) ### 新增應用程式集區 ![](https://i.imgur.com/qFHqp0T.png) ### CLR版本其他版本用來執行舊版 .NET,.NET Core選擇沒有受控碼,沒有受控碼無須理會受控管線模式 ![](https://i.imgur.com/yZwFyFH.png) ### 通常部署檔案會放至C:\inetpub\wwwroot資料夾內(IIS預設位址) ![](https://i.imgur.com/VAb88k3.png) ### 另外設定部署資料夾D:\skytekdnc\web ![](https://i.imgur.com/XcjYXg5.png) ### 在Default Web Site按右鍵新增應用程式 ![](https://i.imgur.com/G4EVCx6.png) ### 新增應用程式設定 別名:自訂網站server name,例`http://localhost/SkyTekDNC` 應用程式集區:選擇剛建立好的SkyTekDNC 實體路徑:選擇IIS部屬資料夾 ![](https://i.imgur.com/cquQxOh.png) ### 按下確定後出現SkyTekDNC ![](https://i.imgur.com/qC4FkY4.png) ### 之後對要部屬的專案按右鍵,選擇發佈(Publish) ![](https://i.imgur.com/CkGMRdy.png) ### 選擇部屬到資料夾 ![](https://i.imgur.com/KXQhXRP.png) ### 發布資料路徑放在專案根目錄 ![](https://i.imgur.com/EWtvtyV.png) ### 此時尚未發佈,只是確定發佈設定檔,完成後關閉 ![](https://i.imgur.com/gLLR3fY.png) ### 點擊右上發佈按鈕 ![](https://i.imgur.com/Zu3HtMm.png) ### 由輸出視窗可看見發佈已成功 ![](https://i.imgur.com/3S8Wn9e.png) ### 點擊目標位置的超連結,可直接開啟發佈資料夾,將資料夾內的檔案放入IIS部屬資料夾(`D:\skytekdnc\web`) ![](https://i.imgur.com/hhpp4NE.png) ### 透過瀏覽即可開啟網站,但是開啟時會有錯誤訊息 ![](https://i.imgur.com/nAIGB5J.png) ### 需要幫IIS安裝模組,依使用的 .NET版本選擇要下載的模組版本(目前為.NET7.0),模組下載位址: [**在使用 IIS 的 Windows 上裝載 ASP.NET Core**](https://learn.microsoft.com/zh-tw/aspnet/core/host-and-deploy/iis/?view=aspnetcore-7.0) ![](https://i.imgur.com/0UHY43s.png) ### 安裝完後,即可開啟網站,當前只有API,請在網址後輸入api/controllers名稱 ![](https://i.imgur.com/9aEplCn.png) ### 專案開發測試時是以Debug模式執行,但是要部屬到Server時不需要使用到Debug模式的許多功能檔案,所以使用發佈取得乾淨的Release資料夾,減少Server的負擔 ![](https://i.imgur.com/jjxJPCQ.png) ## SQL Server帳號設定步驟 參考: [**在本機建立sql server 及sa登入**](https://dotblogs.com.tw/chis_itnote/2017/10/31/sql) ## EF Core Power Tools使用 ### [下載EF Core Power Tools](https://dotblogs.com.tw/chis_itnote/2017/10/31/sql) 使用EF Core Power Tools,可協助Entity Framework Core框架的使用 Entity Framework Core:使用模型來執行資料存取,可以大幅減少開發時期大量的資料庫語法撰寫時間,並且能透過強型別來取得與操作物件資料,提供兩種開發方式 Database First 與 Code First,我們使用Database First開發方式 Database First:先手動建立 Database,再用 Database 生成 Model Code First:先手動 create Data Model,再用 Model 建立 Database 參考: [**Entity Framework Core**](https://learn.microsoft.com/zh-tw/ef/core/) ### 安裝完畢後可在專案點擊右鍵→EF Core Power Tools→Reverse Engineer(反向工程) ![](https://i.imgur.com/l5e2aNi.png) ### 進入資料連線頁面,上方下拉式選單可以選擇要連線的資料庫,EF Core version依專案所使用的 .NET版本選擇,一開始尚未有資料庫設定,點擊Add... ![](https://i.imgur.com/3Sey4lI.png) ### 選擇Add Database Connection設定資料庫連線 ![](https://i.imgur.com/I0z8T1a.png) ### 如果SQL Server已經設定完成,可以正常登入的話,就可以開始設定EF Core資料庫連線 ![](https://i.imgur.com/5HgMIkU.png) ### DB Connection設定 資料來源:可以選取要使用的DB系統,我們使用SQL Server 伺服器名稱:選擇註冊好的伺服器名稱 驗證:常用的有Windows以及SQL Server驗證,如果前面有設定好SQL Server驗證就可以使用,並**勾選儲存密碼**(影響之後產生的Connection String) 如果設定無誤,在選取或輸入資料庫名稱的下拉式選單可以成功看見DB,選取要使用的DB ![](https://i.imgur.com/hKb6gbp.png) ### 設定完成,點擊OK ![](https://i.imgur.com/lfq16iw.png) ### 可以選擇要使用的資料庫結構Table、Stored Procedures等,可選擇需要的項目,如果Table圖案上有三角形警告,代表該Table沒有設定Primary Key,選擇完畢後點擊OK ![](https://i.imgur.com/oYlQ7gT.png) ### 反向工程設定 `Context name`:要產生的Context Class名稱,可維持預設 `Namespace`:專案Namespace名稱,可維持預設 `EntityTypes path`:產生後的EntityTypes Class(資料庫Table)放置資料夾名稱,可維持預設 `What to generate`:要產生的物件,需要EntityTypes以及DBContext Class,可維持預設 下方需勾選三個選項: `Use table and column names directly from database`,讓EntityTypes Class內的項目名稱與DB Table schema名稱同步,不勾選則依照C#命名習慣,例std_key→StdKey **`Include connection string in generated code`**,產生連線字串,務必勾選,不然下次專案無法成功連線資料庫 `Install the EF Core provider package in the project`,讓專案安裝EF Core所需相關套件,第一次勾選即可 設定完畢,點擊OK ![](https://i.imgur.com/QTEqIvl.png) ### 專案內會多出Models資料夾,資料夾內有DBContext Class和EntityTypes Class,反向工程無法漸進式更新,所以一但DB有更動結構的話,需用EF Core Tool重新建立DBContext Class和EntityTypes Class,或者使用手動更新 ![](https://i.imgur.com/6ZlMiJG.png) ### DBContext Class `DbSet< EntityType >`:繼承自DbContext Class的型別,用來追蹤DB的資料異動 `OnModelCreating()`:建立每個EntityTypes Class內每個項目的設定,依照DB Table schema設定 ![](https://i.imgur.com/mRp5cy3.png) ### EntityTypes Class partial:修飾詞,部分類別,允許將Class定義分成多個實體檔案,compiler時會遇到相同Class Name且帶有此修飾詞時,會將這些Class區段結合起來,可想成擴充 ![](https://i.imgur.com/Br0kut8.png) ### 沒問題後,再專案點擊右鍵,選擇建置(Build) ![](https://i.imgur.com/QlF4ACi.png) ### 建置完成後可在輸出視窗看到結果 ![](https://i.imgur.com/VwjTTPR.png) ### 成功後,在專案Controllers資料夾按右鍵→加入→控制器(Controller) ![](https://i.imgur.com/m7bw4GE.png) ### 新增Scaffold項目,選擇使用Entity Framework的API Controller,點選加入 ![](https://i.imgur.com/0BTzN4a.png) ### 新增的API Controller設定 模型類別:要操作的EntityTypes DbContext類別:選擇要使用的DbContext 控制器名稱:新增的Controller名稱,預設與要操作的EntityTypes名稱一樣,可修改,但是後面必須是Controller結尾 點擊新增 ![](https://i.imgur.com/Tf0j68Z.png) ### 新增成功 ![](https://i.imgur.com/FEIxii4.png) ### 反向工程完成後,會在DbContext Class產生連線字串設定,在`optionsBuilder.UseSqlServer()`裡的就是我們要的Connection String ![](https://i.imgur.com/s8mv8I7.png) ### 先將Connection String放入appsettings.json裡方便之後引用 SQL Server有自行簽署的憑證,連線DB時預設的用戶端不接受此憑證,運行時會出現憑證錯誤 解決方式:需在原先產生的Connection String需加入`TrustServerCertificate=true;` (忽略憑證有效性檢查) 設定完後,將原先DbContext Class連線字串設定刪除 參考: [**連接字串**](https://learn.microsoft.com/zh-tw/ef/core/miscellaneous/connection-strings) ![](https://i.imgur.com/ahUBhQD.png) ### 之後在Program.cs將連線字串註冊,有需要使用到時就可以直接呼叫 ![](https://i.imgur.com/j8OC6L1.png) ### 相依性注入 Controller開頭會先呼叫資料庫物件 ```csharp= private readonly eduContext _context; ``` 在類別建構函式進行相依性注入 ```csharp= public class s30_studentController : ControllerBase //類別名稱:繼承類別 { public s30_studentController(eduContext context) //建構函式 { _context = context; } } ``` 當資料庫物件使用完畢,例如Http Response ─ Request流程結束,便刪除此物件,自動幫你管理生命週期 參考: [**筆記 - 不可不知的 ASP.NET Core 依賴注入**](https://blog.darkthread.net/blog/aspnet-core-di-notes/) [**相依性注入起手式 - 「何謂相依」**](https://www.dotblogs.com.tw/armycoding/2020/01/26/what-is-the-dependency) ![](https://i.imgur.com/zSqVbHq.png) ### Controller內會先建置好四種Action HttpGet:產生Select語法 HttpPut:產生Update語法 HttpPost:產生Insert語法 HttpDelete:產生Delete語法 這樣就產生了基礎了API ![](https://i.imgur.com/6tRHRvp.png)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully