標籤:blog http ar io 使用 sp for on 檔案
一、建立新項目
二、添加模型檔案
三、添加完後,在設計面板空白處右擊,建立一個實體
實體集(B) 這裡的名稱會是對應的資料庫表名稱!!! ,開始不知道這是什麼,產生後才知道表名是這個,以後注意點就行。
四、添加實體純量屬性(Scalar Property)
你現在已經完成一個簡單的概念性的模型。從模型產生資料庫,還需要做點事。
五、你現在已經完成一個簡單的概念性的模型。從模型產生資料庫,還需要做點事。
1、右擊設計介面空白處,選擇屬性,修改資料庫架構名稱為Chapter2,修改實體容器名為Recipe1Context
2、從模型產生資料庫,右擊設計介面選擇根據模型產生資料庫
選擇要建立資料庫的伺服器,和要建立的資料庫名稱,這裡資料庫名稱命名為:EF6Recipes
選擇剛才建立的資料庫
選擇EF版本
EF工具根據剛才建立的模型產生SQL指令碼
點擊完成後,VS預設開啟剛才產生的指令碼,點擊執行按鈕建立表結構
開啟 SQL Server Management Studio,查看已經產生的資料庫
六、操作模型:插入、檢索資料
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication3{ class Program { static void Main(string[] args) { using (var context = new Recipe1Context()) { var person = new Person { FirstName = "Robert", MiddleName = "Allen", LastName = "Doe", PhoneNumber = "867-5309" }; context.PersonSet.Add(person); person = new Person { FirstName = "John", MiddleName = "K.", LastName = "Smith", PhoneNumber = "867-5309" }; context.PersonSet.Add(person); person = new Person { FirstName = "Kathy", MiddleName = "Anne", LastName = "Ryan", PhoneNumber = "867-5309" }; context.PersonSet.Add(person); context.SaveChanges(); } using (var context = new Recipe1Context()) { foreach (var person in context.PersonSet) { Console.WriteLine("{0} {1} {2}, Phone: {3}", person.FirstName, person.MiddleName, person.LastName, person.PhoneNumber); } Console.ReadKey(); } } }}
運行輸出結果:
查看資料庫記錄:
2-1. Creating a Simple Model 使用圖形介面設計器建立一個簡單的模型