注:本文提到的代碼示例下載地址> How to using Entity Framework DB first in ASP.NET Corehtml
首先爲你們提醒一點,.NET Core和經典.NET Framework的Library是不通用的,包括Entity Framework!sql
哪怎麼辦? 別急,微軟爲.NET Core發佈了.NET Core版本的Entity Framework,具體配置方法與經典.NET Framework版本的稍有區別,下面的內容就爲帶領你們在ASP.NET Core中應用Entity Framework DB first。數據庫
注:目前部分工具處於Preview版本,正式版本可能會稍有區別。 json
前期準備:app
1.推薦使用VS2015 Update3做爲你的IDE,下載地址:www.visualstudio.comide
2.你須要安裝.NET Core的運行環境以及開發工具,這裏提供VS版:www.microsoft.com/net/core工具
3.你須要有一個Sql Server數據庫。開發工具
結構應該是這樣的。ui
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)
建立項目spa
在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中添加以下配置:
"tools": { "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final", …………. }
這是VS會自動下載對應的包至你的本地,目前這個仍是preview版本,正式版請關注:https://docs.efproject.net/en/latest/intro.html
生成數據庫Mapping
在Pack Manager Console中於運行以下命令:
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並打開它,你會發現一個以下方法,
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方法,完整的代碼應該是這樣:
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};" },
完整的代碼應該像這樣:
{ "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");
完整的代碼應該是這樣:
public void ConfigureServices(IServiceCollection services) { //config the db connection string TestNetCoreEFContext.ConnectionString = Configuration.GetConnectionString("TestNetCoreEF"); // Add framework services. services.AddMvc(); }
關於調用Entity Framework
真的,相信我,跟以前一毛同樣,真的一毛同樣。
Models.TestNetCoreEFContext context = new Models.TestNetCoreEFContext(); var StudentList = context.Student.ToList();
最後:完整的代碼Sample以及如何運行它,請訪問:How to using Entity Framework DB First in ASP.NET Core