環境與版本
作業系統:Window10 64x 版本:2004
開發軟件(IDE):Visual Studio 2019 Community
.NET版本:ASP.NET Core 3.1
專案架構:MVC
資料庫產品版本:SQL Server 2014 Express Edition (64-bit)
資料庫版本編號:12.0.2000.8
範本參考
繼上篇[C#學習]ASP.NET CORE MVC 模型(MODELS) 使用與範本 進行延續學習使用範本
MvcMovieContext 物件
- 會處理連線到資料庫
- 將 Movie 物件對應至資料庫記錄的工作。
在 Startup.cs 檔案的 ConfigureServices 方法中,以相依性插入容器登錄資料庫內容,第5、6行程式碼:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<MvcMovieContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MvcMovieContext")));
}ASP.NET Core 組態系統會讀取 ConnectionString。 對於本機開發,它會從 appsettings.json 檔案取得連接字串:
"ConnectionStrings": {
"MvcMovieContext": "Server=(localdb)\\mssqllocaldb;Database=MvcMovieContext-2;Trusted_Connection=True;MultipleActiveResultSets=true"
}SQL Server Express LocalDB
LocalDB 為輕量版的 SQL Server Express Database Engine,鎖定程式開發為其目標。 LocalDB 會依需求啟動,並以使用者模式執行,因此沒有複雜的組態。 根據預設,LocalDB 資料庫會在C:/Users/{user} 目錄中建立 .mdf 檔案。
請注意ID 旁的索引鍵圖示。 根據預設,EF 會將名為 ID 的屬性設為主索引鍵。
植入資料庫
接下來是使用市場上常說到的Code First的方式,在 Models 資料夾中建立 SeedData 的新類別。 使用下列程式碼取代產生的程式碼:
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MvcMovie.Data;
using System;
using System.Linq;
namespace MvcMovie.Models
{
public static class SeedData
{
public static void Initialize(IServiceProvider serviceProvider)
{
using (var context = new MvcMovieContext(
serviceProvider.GetRequiredService<
DbContextOptions<MvcMovieContext>>()))
{
// Look for any movies.
if (context.Movie.Any())
{
return; // DB has been seeded
}
context.Movie.AddRange(
new Movie
{
Title = "When Harry Met Sally",
ReleaseDate = DateTime.Parse("1989-2-12"),
Genre = "Romantic Comedy",
Price = 7.99M
},
new Movie
{
Title = "Ghostbusters ",
ReleaseDate = DateTime.Parse("1984-3-13"),
Genre = "Comedy",
Price = 8.99M
},
new Movie
{
Title = "Ghostbusters 2",
ReleaseDate = DateTime.Parse("1986-2-23"),
Genre = "Comedy",
Price = 9.99M
},
new Movie
{
Title = "Rio Bravo",
ReleaseDate = DateTime.Parse("1959-4-15"),
Genre = "Western",
Price = 3.99M
}
);
context.SaveChanges();
}
}
}
}如果資料庫中有任何電影,則SeedData初始設定式會返回,而且不會新增任何電影。
if (context.Movie.Any())
{
return; // DB has been seeded.
}新增SeedData .cs在Program.cs初始設定式
Program.cs ,在Main 進入點修改以下7~26行內容。
namespace MvcMovie
{
public class Program
{
public static void Main(string[] args)
{
//CreateHostBuilder(args).Build().Run();
var host = CreateHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
SeedData.Initialize(services);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred seeding the DB.");
}
}
host.Run();
}
}
}範本參考
參考資料:
第5部分:使用 ASP.NET Core MVC 應用程式中的資料庫:https://docs.microsoft.com/zh-tw/aspnet/core/tutorials/first-mvc-app/working-with-sql?view=aspnetcore-5.0&tabs=visual-studio

Pingback: [C#學習]ASP.NET Core MVC 使用Mysql資料庫 使用與範本 - Steven玄
Pingback: [C#學習]ASP.NET Core MVC LINQ to Entities 的CRUD - Steven玄