溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

在ASP.NET Core中應(yīng)用Entity Framework的案例

發(fā)布時(shí)間:2021-02-08 10:12:15 來源:億速云 閱讀:221 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)在ASP.NET Core中應(yīng)用Entity Framework的案例,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

首先為大家提醒一點(diǎn),.NET Core和經(jīng)典.NET Framework的Library是不通用的,包括Entity Framework!

哪怎么辦? 別急,微軟為.NET Core發(fā)布了.NET Core版本的Entity Framework,具體配置方法與經(jīng)典.NET Framework版本的稍有區(qū)別,下面的內(nèi)容就為帶領(lǐng)大家在ASP.NET Core中應(yīng)用Entity Framework DB first。

注:目前部分工具處于Preview版本,正式版本可能會(huì)稍有區(qū)別。

 前期準(zhǔn)備:

1.推薦使用VS2015 Update3作為你的IDE。

2.你需要安裝.NET Core的運(yùn)行環(huán)境以及開發(fā)工具。

3.你需要有一個(gè)Sql Server數(shù)據(jù)庫(kù)。

結(jié)構(gòu)應(yīng)該是這樣的。

CREATE DATABASE TestNetCoreEF 
GO 
USE TestNetCoreEF 
GO 
CREATE TABLE Student( 
  ID int identity primary key, 
  Name nvarchar(50), 
  Age int 
) 
 
INSERT INTO Student VALUES('Bear',18) 
INSERT INTO Student VALUES('Frank',20)

創(chuàng)建項(xiàng)目

在VS中新建項(xiàng)目,項(xiàng)目類型選在ASP.NET Core Web Application (.NET Core),輸入項(xiàng)目名稱為TestEFInNetCore

在ASP.NET Core中應(yīng)用Entity Framework的案例

接下來選擇Web Application, 右側(cè)身份認(rèn)證選擇:No Authentication

在ASP.NET Core中應(yīng)用Entity Framework的案例

安裝Entity Framework

打開Tool->NuGet Package Manager->Package Manager Console

在ASP.NET Core中應(yīng)用Entity Framework的案例

在Pack Manager Console中運(yùn)行如下命令:

  Install-Package Microsoft.EntityFrameworkCore.SqlServer

  Install-Package Microsoft.EntityFrameworkCore.Tools –Pre

  Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design

打開Project.json,在節(jié)點(diǎn)tool中添加如下配置:

"tools": { 
  "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final", 
  …………. 
}

這是VS會(huì)自動(dòng)下載對(duì)應(yīng)的包至你的本地,目前這個(gè)還是preview版本,正式版請(qǐng)關(guān)

注:https://docs.efproject.net/en/latest/intro.html

生成數(shù)據(jù)庫(kù)Mapping

在Pack Manager Console中于運(yùn)行如下命令:

Scaffold-DbContext "{Your DB connect string}" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

{Your DB connect string}:你的數(shù)據(jù)庫(kù)連接字符串

Microsoft.EntityFrameworkCore.SqlServer:目標(biāo)數(shù)據(jù)庫(kù)為Sql Server

-OutputDir Models: 生成的文件的存放目錄,目前目錄是根目錄下的Models目錄

之后引擎會(huì)試圖連接你的SQL Server 數(shù)據(jù)庫(kù),并生成文件在你指定的目錄里。

在目錄中找到一個(gè)***Context.cs并打開它,你會(huì)發(fā)現(xiàn)一個(gè)如下方法,

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
  #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
  optionsBuilder.UseSqlServer(@"{your sql connect string}");
}

如自動(dòng)生成代碼里所寫的warning一樣,我們不應(yīng)該把連接字符串放在這里。接下來的工作,讓我們來從appsettings.json中讀取配置。

在***Context.cs中添加一個(gè)屬性用來存放ConnectionString,另外我們需要重寫OnConfiguring方法,完整的代碼應(yīng)該是這樣:

public static string ConnectionString { get; set; } 
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
{ 
  optionsBuilder.UseSqlServer(ConnectionString); 
}

打開appSetting.json,添加如下代碼:

"ConnectionStrings": { 
  "TestNetCoreEF": "Data Source={your sql server host address};Initial Catalog=TestNetCoreEF;user id={your username};password={your password};" 
},

完整的代碼應(yīng)該像這樣:

{ 
  "ConnectionStrings": { 
    "TestNetCoreEF": "Data Source={your sql server host address};Initial Catalog=TestNetCoreEF;user id={your username};password={your password};" 
  }, 
  "Logging": { 
    "IncludeScopes": false, 
    "LogLevel": { 
      "Default": "Debug", 
      "System": "Information", 
      "Microsoft": "Information" 
    } 
  } 
}

打開 Startup.cs,在ConfigureServices(IServiceCollection services)方法中添加如下代碼:

TestNetCoreEFContext.ConnectionString = Configuration.GetConnectionString("TestNetCoreEF");

完整的代碼應(yīng)該是這樣:

public void ConfigureServices(IServiceCollection services) 
{ 
  //config the db connection string 
  TestNetCoreEFContext.ConnectionString = Configuration.GetConnectionString("TestNetCoreEF"); 
 
  // Add framework services. 
  services.AddMvc(); 
}

關(guān)于調(diào)用Entity Framework

真的,相信我,跟之前一毛一樣,真的一毛一樣。

Models.TestNetCoreEFContext context = new Models.TestNetCoreEFContext();

var StudentList = context.Student.ToList();

關(guān)于“在ASP.NET Core中應(yīng)用Entity Framework的案例”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI