2-1. Creating a Simple Model 使用圖形介面設計器建立一個簡單的模型

來源:互聯網
上載者:User

標籤: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 使用圖形介面設計器建立一個簡單的模型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.