資源簡介
一、EF Code First簡介
EntityFramework 代碼優先
二、EF Code First第一個簡單實例
1、開發環境及數據庫說明
開發環境:Visual Studio 2010 Ultimate sp1 Sql Server 2008 R2
數據庫:Northwind
2、實例代碼結構
結構說明:
App:控制臺應用程序
Data:數據訪問
Domain:實體類
3、安裝Entity Framework
在Visual Studio編輯器中點擊Tools -> Library Package Manager -> Package Manager Console,在Package Manager Console窗口中執行下面語句,安裝最新版Entity Framework。
PM> Install-Package EntityFramework
App層和Data層分別添加對EntityFramework的引用:
在App層安裝EntityFramework之后,將自動添加App.config和packages.config文件。
App.config配置Entity Framework版本信息及數據庫連接信息,修改其中數據連接信息以適應本地實際環境。
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <configSections> 4 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 5 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 6 </configSections> 7 <entityFramework> 8 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> 9 <parameters> 10 <!--<parameter value="Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True" />--> 11 <parameter value="Data Source=(local); User ID=sa; Password=; MultipleActiveResultSets=True" /> 12 </parameters> 13 </defaultConnectionFactory> 14 </entityFramework> 15 </configuration>
packages.config現實當前項目使用的package:
1 <?xml version="1.0" encoding="utf-8"?> 2 <packages> 3 <package id="EntityFramework" version="4.3.1" /> 4 </packages>
4、實例代碼
Domain中Category.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Northwind.Domain.Entities 7 { 8 public class Category 9 { 10 /// <summary> 11 /// 分類ID 12 /// </summary> 13 public int CategoryID { get; set; } 14 15 /// <summary> 16 /// 分類名稱 17 /// </summary> 18 public string CategoryName { get; set; } 19 } 20 }
Data中NorthwindContext.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.Data.Entity; 7 8 using Northwind.Domain.Entities; 9 10 namespace Northwind.Data 11 { 12 public class NorthwindContext : DbContext 13 { 14 public DbSet<Category> Categories { get; set; } 15 } 16 }
App中Program.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using Northwind.Data; 7 using Northwind.Domain.Entities; 8 9 namespace Northwind.App 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 Category c = new Category() { CategoryName = "電子數碼" }; 16 17 using (NorthwindContext db = new NorthwindContext()) 18 { 19 db.Categories.Add(c); 20 db.SaveChanges(); 21 } 22 23 Console.WriteLine("Finish"); 24 Console.ReadKey(); 25 } 26 } 27 }
5、運行說明
由于在上面的數據庫連接字符串中并未包含指定的數據庫名稱,運行成功之后,將在本地數據引擎中創建如下數據庫和表:
數據庫名稱:Northwind.Data.NorthwindContext
表名稱:Categories
6、示例代碼附件
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?Northwind.Data;
using?Northwind.Domain.Entities;
namespace?Northwind.App
{
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????Category?c?=?new?Category()?{?CategoryName?=?“電子數碼“?};
????????????using?(NorthwindContext?db?=?new?NorthwindContext())
????????????{
????????????????db.Categories.Add(c);
????????????????db.SaveChanges();
????????????}
????????????Console.WriteLine(“Finish“);
????????????Console.ReadKey();
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????889??2012-03-25?19:26??Northwind\App\App.config
?????文件???????3339??2012-03-25?19:12??Northwind\App\App.csproj
?????文件????????490??2010-03-17?22:39??Northwind\App\bin\Debug\App.vshost.exe.manifest
?????文件????1060640??2012-03-25?11:51??Northwind\App\bin\Debug\Entityfr
?????文件????1107427??2012-03-25?11:51??Northwind\App\bin\Debug\Entityfr
?????文件???????5120??2012-03-25?19:33??Northwind\App\bin\Debug\Northwind.App.exe
?????文件????????889??2012-03-25?19:26??Northwind\App\bin\Debug\Northwind.App.exe.config
?????文件??????13824??2012-03-25?19:33??Northwind\App\bin\Debug\Northwind.App.pdb
?????文件??????11600??2012-03-25?19:34??Northwind\App\bin\Debug\Northwind.App.vshost.exe
?????文件????????889??2012-03-25?19:26??Northwind\App\bin\Debug\Northwind.App.vshost.exe.config
?????文件???????4608??2012-03-25?19:12??Northwind\App\bin\Debug\Northwind.Data.dll
?????文件???????7680??2012-03-25?19:12??Northwind\App\bin\Debug\Northwind.Data.pdb
?????文件???????4608??2012-03-25?19:12??Northwind\App\bin\Debug\Northwind.Domain.dll
?????文件???????7680??2012-03-25?19:12??Northwind\App\bin\Debug\Northwind.Domain.pdb
?????文件????????764??2012-03-25?19:34??Northwind\App\obj\x86\Debug\App.csproj.FileListAbsolute.txt
?????文件???????6154??2012-03-25?19:33??Northwind\App\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????5120??2012-03-25?19:33??Northwind\App\obj\x86\Debug\Northwind.App.exe
?????文件??????13824??2012-03-25?19:33??Northwind\App\obj\x86\Debug\Northwind.App.pdb
?????文件??????12602??2012-03-25?19:12??Northwind\App\obj\x86\Debug\ResolveAssemblyReference.cache
?????文件????????118??2012-03-25?11:51??Northwind\App\packages.config
?????文件????????603??2012-03-25?23:50??Northwind\App\Program.cs
?????文件???????1338??2012-03-25?11:38??Northwind\App\Properties\AssemblyInfo.cs
?????文件????1060640??2012-03-25?11:51??Northwind\Data\bin\Debug\Entityfr
?????文件????1107427??2012-03-25?11:51??Northwind\Data\bin\Debug\Entityfr
?????文件???????4608??2012-03-25?19:12??Northwind\Data\bin\Debug\Northwind.Data.dll
?????文件???????7680??2012-03-25?19:12??Northwind\Data\bin\Debug\Northwind.Data.pdb
?????文件???????4608??2012-03-25?19:12??Northwind\Data\bin\Debug\Northwind.Domain.dll
?????文件???????7680??2012-03-25?19:12??Northwind\Data\bin\Debug\Northwind.Domain.pdb
?????文件???????2828??2012-03-25?19:12??Northwind\Data\Data.csproj
?????文件????????308??2012-03-25?23:48??Northwind\Data\NorthwindContext.cs
............此處省略66個文件信息
評論
共有 條評論