在本教程中,你將使用 Entity Framework Core 構(gòu)建執(zhí)行基本數(shù)據(jù)訪問的 ASP.NET Core MVC 應(yīng)用程序。 該教程使用遷移基于此數(shù)據(jù)模型創(chuàng)建數(shù)據(jù)庫(kù)。
你可在 Windows 上使用 Visual Studio 2017,或在 Windows、macOS 或 Linux 上使用 .NET Core CLI 來學(xué)習(xí)本教程。
在 GitHub 上查看此文章的示例:
安裝以下軟件:
警告:如果你使用“單獨(dú)用戶帳戶”(而不是“無”)進(jìn)行身份驗(yàn)證,Entity Framework Core 模型會(huì)添加到 Models\IdentityModel.cs
中的項(xiàng)目。 使用在本教程中學(xué)習(xí)的技巧,可以選擇添加第二個(gè)模型,或者擴(kuò)展此現(xiàn)有模型以包含實(shí)體類。
要安裝 EF Core,請(qǐng)為要作為目標(biāo)對(duì)象的 EF Core 數(shù)據(jù)庫(kù)提供程序安裝程序包。 有關(guān)可用提供程序的列表,請(qǐng)參閱數(shù)據(jù)庫(kù)提供程序。
對(duì)于本教程,無需安裝提供程序包,因?yàn)楸窘坛淌褂?SQL Server。 SQL Server 提供程序包包含在 Microsoft.AspnetCore.App 元包中。
定義構(gòu)成模型的上下文類和實(shí)體類。
右鍵單擊“Models”文件夾,然后選擇“添加”>“類”。
輸入“Model.cs”作為名稱,然后單擊“確定”。
將此文件的內(nèi)容替換為以下代碼:
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
namespace EFGetStarted.AspNetCore.NewDb.Models
{
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions<BloggingContext> options)
: base(options)
{ }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public ICollection<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
}
生產(chǎn)應(yīng)用通常會(huì)將每個(gè)類放在單獨(dú)的文件中。 為簡(jiǎn)單起見,本教程將這些類放在一個(gè)文件中。
若要使 BloggingContext 可用于 MVC 控制器,請(qǐng)?jiān)?nbsp;Startup.cs 中將其注冊(cè)為服務(wù)。
在應(yīng)用程序啟動(dòng)過程中,通過依賴關(guān)系注入 注冊(cè)服務(wù)(如 BloggingContext),以便能夠通過構(gòu)造函數(shù)的參數(shù)和屬性向使用服務(wù)的組件(如 MVC 控制器)自動(dòng)提供該服務(wù)。
在 Startup.cs 中,添加以下 using
語(yǔ)句:
using EFGetStarted.AspNetCore.NewDb.Models;
using Microsoft.EntityFrameworkCore;
將以下突出顯示的代碼添加到 ConfigureServices
方法:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
var connection = @"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.AspNetCore.NewDb;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<BloggingContext>
(options => options.UseSqlServer(connection));
// BloggingContext requires
// using EFGetStarted.AspNetCore.NewDb.Models;
// UseSqlServer requires
// using Microsoft.EntityFrameworkCore;
}
生產(chǎn)應(yīng)用通常會(huì)將連接字符串放在配置文件或環(huán)境變量中。 為簡(jiǎn)單起見,本教程在代碼中定義它。有關(guān)詳細(xì)信息,請(qǐng)參閱連接字符串 。
以下步驟使用遷移創(chuàng)建數(shù)據(jù)庫(kù)。
“工具”>“NuGet 包管理器”>“包管理器控制臺(tái)”
運(yùn)行以下命令:
Add-Migration InitialCreate
Update-Database
如果收到錯(cuò)誤,指出 The term 'add-migration' is not recognized as the name of a cmdlet
,請(qǐng)關(guān)閉并重新打開 Visual Studio。
Add-Migration
命令為遷移搭建基架,以便為模型創(chuàng)建一組初始表。 Update-Database
命令創(chuàng)建數(shù)據(jù)庫(kù)并向其應(yīng)用新的遷移。
生成 Blog 實(shí)體控制器和視圖的基架。
基架引擎創(chuàng)建以下文件:
更多建議: