NetCore2.0 RozarPage自動產生增刪改查

來源:互聯網
上載者:User

標籤:logging   connect   context   建立檔案夾   gen   mod   script   spn   addm   

 

 

 

原文連結:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/web-api-help-pages-using-swagger

上面的只是原文的上面的連結,右邊的

沒有連結,不能點擊,不知道為什麼。不過大家可以點擊下面的下載PDF,下載後開啟看就可以了

準備工作

VS2017需要15.3以後的版本,且安裝完NetCore2.0 SDK

Net Core 2.0 SDK下載連結:https://www.microsoft.com/net/core#windowscmd

1、開啟VS2017,建立項目----.Net Core----AspNet Core Web應用程式

然後選擇Web應用程式,注意選擇Net Core2.0

2、項目建好後,右鍵項目----添加--建立檔案夾--命名為Models

3、Models裡面添加兩個類一個實體Movie,一個DbContext類MovieContext

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5  6 namespace RozarPage.Models 7 { 8     public class Movie 9     {10         public int ID { get; set; }11 12         public string Title { get; set; }13 14         public DateTime ReleaseDate { get; set; }15 16         public string Genre { get; set; }17 18         public decimal Price { get; set; }19     }20 }
 1 using Microsoft.EntityFrameworkCore; 2  3 namespace RozarPage.Models 4 { 5     public class MovieContext : DbContext 6     { 7         public MovieContext(DbContextOptions<MovieContext> options) : base(options) 8         { 9             //10         }11 12         public DbSet<Movie> Movie { get; set; }13     }14 }

4、開啟appsetting.json添加資料庫連接字串

 1 { 2   "Logging": { 3     "IncludeScopes": false, 4     "LogLevel": { 5       "Default": "Warning" 6     } 7   }, 8   "ConnectionStrings": { 9     "Default": "Server=localhost; Database=Rozar; Uid=sa; Pwd=123456; MultipleActiveResultSets=true;"10   }11 }

5、開啟Startup.cs添加資料庫連結服務支援(抱歉,粘貼時縮排變形了)

1 public void ConfigureServices(IServiceCollection services)2         {3             services.AddMvc();4             services.AddDbContext<MovieContext>(options =>5                 options.UseSqlServer(Configuration.GetConnectionString("Default")));6 7         }

6、開啟VS2017----工具----Nuget包管理器----Nuget包管理器控制台

依次執行下面三個命令

PM> Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design -Version 2.0.0
PM> Add-Migration Initial
PM> Update-Database

7、開啟項目所在的檔案夾(檔案夾中有Startup.cs檔案,Program.cs檔案和csproj檔案)按住Shift右鍵----在此處開啟命令視窗

輸入以下命令

dotnet aspnet-codegenerator razorpage -m Movie -dc MovieContext -udl -outDir Pages\Movies --referenceScriptLibraries

Movie是你的實體名稱,MovieContext是你的DbContext名稱,後面的outDir是輸出檔案路徑,後面的 --referenceScriptLibraries是添加一個供編輯和建立頁面的驗證指令碼(就是_ValidationScriptsPartial.cshtml)

 

 運行完畢後會在你的Page\Movies檔案夾中產生增刪改查的cshtml

此時運行你的項目,輸入http://localhost:連接埠號碼/Movies/Create看看你產生的頁面吧

 

NetCore2.0 RozarPage自動產生增刪改查

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.