ASP.NET Core .NET 5 打造WebAPI入門教學(2_2) - 教學上我們先使用LocalDB

Leave a Comment

需要安裝以下套件
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.Design

指令
Scaffold-DbContext "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\Todo.mdf;Integrated Security=True;Connect Timeout=30" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force -CoNtext TodoContext

appsettings.json
{
  "ConnectionStrings": {
    "TodoDatabase": "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=D:\\Todo.mdf;Integrated Security=True;Connect Timeout=30"
}, }

Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<TodoContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("TodoDatabase"))); }

範例下載:下載

0 意見:

張貼留言

技術提供:Blogger.