首先為大家提醒一點(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,下載地址://www.gxlcms.com/softjc/446184.html
2.你需要安裝.NET Core的運(yùn)行環(huán)境以及開發(fā)工具,這里提供VS版://www.gxlcms.com/softs/472362.html
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
接下來選擇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ù)庫(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();
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com