Entity Framework CodeFirst尝试
前言
Code First模式我們稱之為“代碼優先”模式,是從EF4.1開始新建加入的功能。使用Code First模式進行EF開發時開發人員只需要編寫對應的數據類(其實就是領域模型的實現過程),然后自動生成數據庫。這樣設計的好處在于我們可以針對概念模型進行所有數據操作而不必關系數據的存儲關系,使我們可以更加自然的采用面向對象的方式進行面向數據的應用程序開發。
從某種角度來看,其實“Code First”和“Model First”區別并不是太明顯,只是它不借助于實體數據模型設計器,而是直接通過編碼(數據類)方式設計實體模型(這也是為什么最開始“Code First”被叫做“Code Only”的原因)。但是對于EF它的處理過程有所差別,例如我們使用Code First就不再需要EDM文件,所有的映射通過“數據注釋”和“fluent API”進行映射和配置。另外需要注意的是“Code First”并不代表一定就必須通過數據類來定義模型,事實上也可以通過現有數據庫生成數據類。
Code First 簡單使用演示
第一步:還是添加一個控制臺應用程序,然后在此項目中添加兩個簡單的實體類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace CodeFirstTest {public class OrderDetail{public int ID { get; set; }public string Product { get; set; }public int OrderID { get; set; }public virtual Order Order { get; set; }} } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace CodeFirstTest {public class Order{public int ID { get; set; }public string Customer { get; set; }public DateTime OrderDate { get; set; }public virtual List<OrderDetail> OrderDetails { get; set; }} }有了這兩個類之后讓我們定義一個數據庫上下文,有了它我們就可以對數據進行增刪改查操作了,這個類必須繼承于"System.Data.Entity.DbContext”類以賦予它數據操作能力。因此接下來我們需要給這個應用安裝EntityFramework包,因為到目前為止我們并沒有引入EF框架相關的任何內容,我們需要引入EF相關程序集。但是我們有更好的選擇那就是NuGet。通過NuGet進行在線安裝:項目中右鍵選擇"Manage NuGet Packages…”;選擇Online;再選擇“EntityFramework”;然后點擊安裝即可。不了解NuGet的朋友到這里看一下使用 NuGet 管理項目庫。
數據庫上下文操作類:
namespace CodeFirstTest {public class OrderTestContext:DbContext{public DbSet<Order> Orders { get;set;}public DbSet<OrderDetail> OrderDetails { get; set; }} }代碼調用實例
?
static void Main(string[] args){using (var db = new OrderTestContext()){Order Order = new Order();Order.Customer = "aehyok";Order.OrderDate = DateTime.Now;db.Orders.Add(Order);db.SaveChanges();IQueryable<Order> Orders = from Orderes in db.Ordersselect Orderes;foreach (Order O in Orders){Console.WriteLine("OrderID is {0},Customer is {1}", O.ID, O.Customer);}}Console.ReadLine();}?調用效果
?
生成的數據庫在這里,在此我們可以發現針對數據庫,我們沒有做任何的處理,沒有建數據庫,也沒有進行配置,當然它應該會有默認的配置。
接下來我們來看一下,如何來配置。
我們可以在App的配置文件里添加一個數據庫的鏈接字符串
<?xml version="1.0" encoding="utf-8"?> <configuration><configSections><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /></configSections><connectionStrings><add name="CodeFirstTest" connectionString="Data Source=.;Database=CodeFirstTest;UID=sa;PWD=sa123;" providerName="System.Data.SqlClient"></add></connectionStrings><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /></startup><entityFramework><defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"><parameters><parameter value="v11.0" /></parameters></defaultConnectionFactory></entityFramework> </configuration>?里面主要添加了一個數據庫鏈接字符串
針對數據庫上下文操作類的更改
public class OrderTestContext:DbContext{public OrderTestContext(string connectionName): base(connectionName){}public DbSet<Order> Orders { get;set;}public DbSet<OrderDetail> OrderDetails { get; set; }}?主要是添加了一個構造函數帶有一個參數的。然后在調用上也有所調整。
using (var db = new OrderTestContext("CodeFirstTest")){Order Order = new Order();Order.Customer = "aehyok";Order.OrderDate = DateTime.Now;db.Orders.Add(Order);db.SaveChanges();IQueryable<Order> Orders = from Orderes in db.Ordersselect Orderes;foreach (Order O in Orders){Console.WriteLine("OrderID is {0},Customer is {1}", O.ID, O.Customer);}}?主要是針對實例化數據庫操作上下文的
調用結果也是
同時打開Sql Server 發現也有了剛剛在配置文件中配置的那個數據庫
?
?
?
?
轉載于:https://www.cnblogs.com/aehyok/p/3323496.html
總結
以上是生活随笔為你收集整理的Entity Framework CodeFirst尝试的全部內容,希望文章能夠幫你解決所遇到的問題。