欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Entity?Framework?Core基于數(shù)據(jù)模型創(chuàng)建數(shù)據(jù)庫

 更新時間:2022年04月07日 15:06:26   作者:暗斷腸  
這篇文章介紹了Entity?Framework?Core基于數(shù)據(jù)模型創(chuàng)建數(shù)據(jù)庫的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.簡介

使用Entity Framework Core構(gòu)建執(zhí)行基本數(shù)據(jù)訪問的ASP.NET Core MVC應(yīng)用程序。使用遷移(Migrations)基于數(shù)據(jù)模型創(chuàng)建數(shù)據(jù)庫,你可以在Windows上使用Visual Studio 2017 PowerShell或在Windows、macOS或Linux上使用.NET Core CLI來學(xué)習(xí)創(chuàng)建數(shù)據(jù)庫。

2.創(chuàng)建新項目

2.1系統(tǒng)必備

在創(chuàng)建新項目之前都要檢查是否安裝以下軟件:
●具有以下工作負(fù)載的Visual Studio 2017 15.7版或更高版本(Visual Studio必備):
  ○“ASP.NET和Web開發(fā)”(位于“Web 和云”下)
  ○“.NET Core跨平臺開發(fā)”(位于“其他工具集”下)
●.NET Core 2.1 SDK.(Visual Studio、CLI必備)

2.2 創(chuàng)建項目

Core MVC項目可以通過Visual Studio手動來創(chuàng)建,也可以通過在CLI輸入命令行來創(chuàng)建,兩者區(qū)別是前者限制在Windows平臺上創(chuàng)建項目,后者是可以跨平臺創(chuàng)建項目。

2.2.1Visual Studio手動來創(chuàng)建項目

●打開Visual Studio 2017
●“文件”>“新建”>“項目”。
●從左菜單中選擇“其他項目類型”>“Visual Studio 解決方案”。
●點擊新建解決方案右鍵選擇“添加”>“新建項目”>“已安裝”>“Visual C#”>“.NET Core” 。
●選擇“ASP.NET Core Web 應(yīng)用程序”。
●輸入“MyCoreWeb”自定義名稱,然后單擊“確定”。
●在“新建ASP.NET Core Web應(yīng)用程序”對話框中:
  ○確保在下拉列表中選擇“.NET Core”和“ASP.NET Core 2.1”
  ○選擇“Web 應(yīng)用程序(模型視圖控制器)”項目模板
  ○確保將“身份驗證”設(shè)置為“不進(jìn)行身份驗證”
  ○單擊“確定”
警告:如果你使用“單獨用戶帳戶”(而不是“無”)進(jìn)行身份驗證,Entity Framework Core模型會添加到Models\IdentityModel.cs中的項目。

2.2.2通過在CLI輸入命令行來創(chuàng)建項目

運行以下命令以創(chuàng)建MVC項目:

dotnet new mvc -n MyCoreWeb

更改為項目目錄,你輸入的下一個命令需要針對新項目運行:

cd MyCoreWeb

3.安裝Entity Framework Core

要安裝EF Core,請為要作為目標(biāo)對象的EF Core數(shù)據(jù)庫提供程序安裝程序包。有關(guān)可用提供程序的列表,請參閱數(shù)據(jù)庫提供程序。因為我本機(jī)是用SqlServer數(shù)據(jù)庫,所以可以通過以下兩種方式安裝EF Core。

3.1在包管理器控制臺輸入命令來安裝程序包(“工具”>“NuGet包管理器”>“程序包管理器控制臺”)

install-package Microsoft.EntityFrameworkCore.SqlServer -Version 2.2.0

3.2通過在CLI輸入命令行來安裝程序包

--更改為項目所在目錄
cd /d D:\Project\MyCoreWeb
--輸入CLI命令安裝程序包
dotnet add package Microsoft.EntityFrameworkCore.SqlServer

4.創(chuàng)建模型

在Models文件夾下創(chuàng)建BloggingContext.cs文件,為了簡單起見,我們都將Blog、Post實體代碼寫在BloggingContext.cs文件中:

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; }
}

5.使用依賴注入注冊上下文

在應(yīng)用程序啟動過程中,通過依賴關(guān)系注入注冊服務(wù)(如 BloggingContext),以便能夠通過構(gòu)造函數(shù)的參數(shù)和屬性向使用服務(wù)的組件(如 MVC 控制器)自動提供該服務(wù)。如果想要在MVC控制器里面調(diào)用BloggingContext.cs,那么就要在Startup.cs中將其注冊為服務(wù)。

public void ConfigureServices(IServiceCollection services)
{
    var connection = @"Server=.;Database=Blogging;Trusted_Connection=True;ConnectRetryCount=0";
    services.AddDbContext<BloggingContext>(options => options.UseSqlServer(connection));
}

為簡單起見,這里把連接字符串直接在代碼中定義。但是通常是會將連接字符串放在配置文件或環(huán)境變量中。例如:

appsettings.json
{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "BloggingDatabase": "Server=.;Database=Blogging;Trusted_Connection=True;"
  }
}

Startup.cs中其注冊的服務(wù)代碼為:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<BloggingContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));
}

6.遷移創(chuàng)建數(shù)據(jù)庫(重點)

這個章節(jié)比較重要,下面讓我們來學(xué)習(xí)下如何遷移創(chuàng)建數(shù)據(jù)庫。

6.1Visual Studio PowerShell手動來創(chuàng)建項目(“工具”>“NuGet包管理器”>“程序包管理器控制臺”)

Add-Migration InitialCreate
Update-Database

如果收到錯誤,指出The term 'add-migration' is not recognized as the name of a cmdlet,請關(guān)閉并重新打開Visual Studio。
Add-Migration命令為遷移搭建基架,以便為模型創(chuàng)建一組初始表。Update-Database命令創(chuàng)建數(shù)據(jù)庫并向其應(yīng)用程序新的遷移。
因為程序包管理器不支持PowerShell 2.0版本的遷移,需要升級到3.0版本,所以這里就暫時演示不了,請大家自行升級3.0或以上版本測試。

或者收到這樣錯誤,無法將“Add-Migration”項識別為 cmdlet、函數(shù)、腳本文件或可運行程序的名稱。

請在PowerShell輸入如下命令行:Import-Module C:\Users\用戶名\.nuget\packages\microsoft.entityframeworkcore.tools\3.1.0\tools\EntityFrameworkCore.psd1,再執(zhí)行如上命令即可。

6.2通過在CLI輸入命令行來遷移

--更改為項目所在目錄
cd /d D:\Project\MyCoreWeb
--遷移搭建基架
dotnet ef migrations add InitialCreate
--創(chuàng)建數(shù)據(jù)庫并向其應(yīng)用程序新的遷移
dotnet ef database update

下面我們來看看遷移創(chuàng)建數(shù)據(jù)庫效果:

到此這篇關(guān)于Entity Framework Core基于數(shù)據(jù)模型創(chuàng)建數(shù)據(jù)庫的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論