ASP.NET Core - SQL Server Configuration
SQLServer configuration in ASP.NET Core
先去
appsettings.json
找到 default connection string
,然後把資料庫的connection string
換成自己的 local connection string
, 這一個 DefaultConnection
Visual Studio自己產生的, DefaultConnection是關鍵字, 按右鍵找到 properties
把它貼在 appsettings.json
的 DefaultConnection
Create Scaffolding for Database (All Tables)
在
Nuget package console
打上 Scaffold-DbContext "Data Source=localhost;Initial Catalog=xxx;Integrated Security=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -dataAnnotations
可以讓 Entity Framework
做全部的事
接下來要
add-migration
, migration: create or update the database based on the model, create the database from the model (entities and context) by adding a migration.
如果要 update database, 就要用
update-database
接下來要讓 middleware 知道哪裡可以存我的資料所以要把
ApplicationDBContext
(這個是default)換成我們自己的database context, 如果網站可以重新啟動的話就表示有成功
在 controllers 的 folder, 按右鍵
add
, 選擇 New controllers mvc entity framework
把要的 database context
選好之後就可以有 scaffolding 的功能, 他會自動產生 action method 而且也會自動產生categories views, 在 categories views 裡面也有相對應的 views 對到 controllers action
留言
張貼留言