原文參考:http://blog.bossma.cn/csharp/code-first-dwith-ado-net-entity-framework-4-1/
.NET的實體架構越來越完善了,前幾天看到Entity Framework 4.1已經正式釋出了,新添加了一種稱為Code First的開發模式。字面上的意思就是代碼優先;按照微軟對于它的說明就是:Code First聚焦于定義你的model類,這些類可以映射到一個現有的資料庫,或者根據這些類生成資料庫,并且提供了資料注解功能和一個易用的API。
下面将對如何使用這種開發模式做一個簡單的說明:
準備:您需要已經安裝VS2010以及
[Column(TypeName = "varchar" )] |
public string CategoryId { get ; set ; } |
[Column( "CategoryName" , TypeName = "nvarchar" )] |
public string Name { get ; set ; } |
public virtual ICollection<Product> Products { get ; set ; } |
public int ProductId { get ; set ; } |
[Column( "ProductName" , TypeName = "nvarchar" )] |
public string Name { get ; set ; } |
public string CategoryId { get ; set ; } |
public virtual Category Category { get ; set ; } |
public string SupplierCode { get ; set ; } |
[Column( "SupplierName" , TypeName = "nvarchar" )] |
public string Name { get ; set ; } |
public DateTime AddTime { get ; set ; } |