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

詳解如何在ASP.NET Core中應(yīng)用Entity Framework

 更新時(shí)間:2016年12月03日 09:19:13   作者:微軟一站式示例代碼庫  
本篇文章主要介紹了如何在ASP.NET Core中應(yīng)用Entity Framework,具有一定的參考價(jià)值,有興趣的可以一起了解一下。

首先為大家提醒一點(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,下載地址:http://www.dbjr.com.cn/softjc/446184.html

2.你需要安裝.NET Core的運(yùn)行環(huán)境以及開發(fā)工具,這里提供VS版:http://www.dbjr.com.cn/softs/472362.html

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

結(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

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

安裝Entity Framework

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

在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ù)庫Mapping

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

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

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

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

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

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

在目錄中找到一個(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();

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論