需要安裝以下套件
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 意見:
張貼留言