首先為大家提醒一點,.NET Core和經典.NET Framework的Library是不通用的,包括Entity Framework!
哪怎么辦? 別急,微軟為.NET Core發布了.NET Core版本的Entity Framework,具體配置方法與經典.NET Framework版本的稍有區別,下面的內容就為帶領大家在ASP.NET Core中應用Entity Framework DB first。
注:目前部分工具處于Preview版本,正式版本可能會稍有區別。
前期準備:
1.推薦使用VS2015 Update3作為你的IDE
2.你需要安裝.NET Core的運行環境以及開發工具
3.你需要有一個Sql Server數據庫。
結構應該是這樣的。
1
2
3
4
5
6
7
8
9
10
11
12
|
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) |
創建項目
在VS中新建項目,項目類型選在ASP.NET Core Web Application (.NET Core),輸入項目名稱為TestEFInNetCore
接下來選擇Web Application, 右側身份認證選擇:No Authentication
安裝Entity Framework
打開Tool->NuGet Package Manager->Package Manager Console
在Pack Manager Console中運行如下命令:
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools –Pre
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design
打開Project.json,在節點tool中添加如下配置:
1
2
3
4
|
"tools" : { "Microsoft.EntityFrameworkCore.Tools" : "1.0.0-preview2-final" , …………. } |
這是VS會自動下載對應的包至你的本地,目前這個還是preview版本,正式版請關
注:https://docs.efproject.net/en/latest/intro.html
生成數據庫Mapping
在Pack Manager Console中于運行如下命令:
1
|
Scaffold-DbContext "{Your DB connect string}" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models |
{Your DB connect string}:你的數據庫連接字符串
Microsoft.EntityFrameworkCore.SqlServer:目標數據庫為Sql Server
-OutputDir Models: 生成的文件的存放目錄,目前目錄是根目錄下的Models目錄
之后引擎會試圖連接你的SQL Server 數據庫,并生成文件在你指定的目錄里。
在目錄中找到一個***Context.cs并打開它,你會發現一個如下方法,
1
2
3
4
5
|
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}" ); } |
如自動生成代碼里所寫的warning一樣,我們不應該把連接字符串放在這里。接下來的工作,讓我們來從appsettings.json中讀取配置。
在***Context.cs中添加一個屬性用來存放ConnectionString,另外我們需要重寫OnConfiguring方法,完整的代碼應該是這樣:
1
2
3
4
5
|
public static string ConnectionString { get ; set ; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(ConnectionString); } |
打開appSetting.json,添加如下代碼:
1
2
3
|
"ConnectionStrings" : { "TestNetCoreEF" : "Data Source={your sql server host address};Initial Catalog=TestNetCoreEF;user id={your username};password={your password};" }, |
完整的代碼應該像這樣:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
{ "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");
完整的代碼應該是這樣:
1
2
3
4
5
6
7
8
|
public void ConfigureServices(IServiceCollection services) { //config the db connection string TestNetCoreEFContext.ConnectionString = Configuration.GetConnectionString( "TestNetCoreEF" ); // Add framework services. services.AddMvc(); } |
關于調用Entity Framework
真的,相信我,跟之前一毛一樣,真的一毛一樣。
1
2
3
|
Models.TestNetCoreEFContext context = new Models.TestNetCoreEFContext(); var StudentList = context.Student.ToList(); |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/onecodeonescript/p/6016140.html