EF Code First學習筆記 初識Code First

來源:互聯網
上載者:User

標籤:

Code First是Entity Framework提供的一種新的編程模型。通過Code First我們可以在還沒有建立資料庫的情況下就開始編碼,然後通過代碼來產生資料庫。

下面通過一個簡單的樣本來瞭解。

 建立一個控制台項目。通過Nuget來擷取Entity Framework。

 增加兩個模型類:

 public class Destination    {        public int DestinationId { get; set; }        public string Name { get; set; }        public string Country { get; set; }        public string Description { get; set; }        public byte[] Photo { get; set; }        public List<Lodging> Lodgings { get; set; }    }    public class Lodging    {        public int LodgingId { get; set; }        public string Name { get; set; }        public string Owner { get; set; }        public bool IsResort { get; set; }        public Destination Destination { get; set; }    } 

再新增Context類:

 public class BreakAwayContext : DbContext    {        public DbSet<Destination> Destinations { get; set; }        public DbSet<Lodging> Lodgings { get; set; }    } 

在Main方法中加入下列代碼:

  static void Main(string[] args)        {            var d = DateTime.Now.Date.ToString("yyyyMM");            var destination = new Destination            {                Country = "Indonesia",                Description = "EcoTourism at its best in exquisite Bali",                Name = "Bali"            };            using (var context = new BreakAwayContext())            {                context.Destinations.Add(destination);                context.SaveChanges();            }            Console.WriteLine("OK");        }

執行成功後開啟資料庫,預設為.\SQLEXPRESS。

我們可以看到,新增了一個名為BreakAway.BreakAwayContext的資料庫。

[Destinations]表裡面也插入了我們剛增加的記錄:

很COOL吧,Code First就是這麼神奇。這裡我們代碼裡面什麼也沒設定,都是Code First預設產生的。通過產生的資料庫庫我們來瞭解一下這些預設設定。

資料庫名:當沒有顯示設定資料連線的時候,預設的資料庫是:.\SQLEXPRESS。如果本地沒有SQLEXPRESS,EF會嘗試LocalDb ((localdb)\v11.0) .\SQLEXPRESS

這個資料庫包含在VS2012中。資料庫的名稱一般是DbContext的“命名空間.類名”,本例中是BreakAway.BreakAwayContext

表名:表名預設為模型類名的複數形式,並且每個表都使用dbo構架建立。這裡產生的就是dbo.Lodgings.

主鍵:Code First會預設將以類似Id結尾來命名的屬性當作主鍵,如ID,Id,本例中的DestinationId都自動化佈建為主鍵。如果該屬性是int類型,Code First會在資料庫中預設將該列設定為自增長。

資料類型:在SQL Server中,字串預設映射成nvarchar(max),byte[]映射成varbinary(max),bool映射成bit,decimal映射成decimal(18, 2),float映射成float。同時因為bool,decimal,float等是實值型別,不能為給他們分配Null值。所產生的資料庫會要求對應的列非空。如Lodgings表中的IsResort

外鍵:Code First檢測到模型間有一對多的關係,會自動在相應表中產生外鍵。在這時,Code First檢測到Destination類中有一個List<Lodging> Lodgings屬性,而在Lodging類中有一個Destination Destination屬性,說明Destination與Lodging是一對多的關係,因而在Lodgings表中產生了外鍵Destination_DestinationId儲存對應的DestinationId。外鍵的命名預設是導覽屬性名(這裡是Destination)_對應主表的主鍵(這裡是DestinationId)。

EF Code First學習筆記 初識Code First

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.