文章目錄
- 1. 建立項目
- 2. 編寫實體類
- 3. 添加EFCodeFirst
- 4. 配置
- 5. 小試牛刀
- 6. 修改Model後,自動更新資料表
- 7. 寫在最後
- 源碼下載
1. 建立項目
開啟VS2010,選擇 檔案>建立>項目,建立ASP.NET MVC3 Web 應用程式,我這裡把它命名為Blog。
2. 編寫實體類
對於一個部落格,一下幾個類應該是必須的吧:
- Post 部落格文章類
- Comment 文章評論類,和Post是一對多的關係
- Category 目錄類,和Post是一對多的關係
- Tag 標籤類,和Post是多對多的關係
- FriendLink 友情連結類
先不考慮管理員之類的東西。 在Model中依次添加上面的類。
namespace Blog.Models{ public class Post { public int ID { get; set; } public int CategoryID { get; set; } public string Title { get; set; } public string Summary { get; set; } public string Alias { get; set; } public string Content { get; set; } public DateTime CreateTime { get; set; } public Category Category { get; set; } public ICollection<Tag> Tags { get; set; } public ICollection<Comment> Coments { get; set; } }}
namespace Blog.Models{ public class Comment { public int ID { get; set; } public int PostID { get; set; } public int Level { get; set; } public int ReplyTo { get; set; } public string UserName { get; set; } public string Email { get; set; } public string Website { get; set; } public string Content { get; set; } public DateTime CreateTime { get; set; } }}
namespace Blog.Models{ public class Category { public int ID { get; set; } public string Name { get; set; } public string Alias { get; set; } public string Description { get; set; } public DateTime CreateTime { get; set; } public ICollection<Post> Posts { get; set; } }}
namespace Blog.Models{ public class Tag { public int ID { get; set; } public string Name { get; set; } public string Alias { get; set; } public DateTime CreateTime { get; set; } public ICollection<Post> Posts { get; set; } }}
namespace Blog.Models{ public class FriendLink { public int ID { get; set; } public string Name { get; set; } public string URL { get; set; } public string Description { get; set; } public DateTime CreateTime { get; set; } }}
3. 添加EFCodeFirst
選擇功能表列的 工具 > Library Package Magager > Package Manager Console
在Package Manager Console中輸入以下命令安裝EFCodeFirst
PM> install-package efcodefirst
安裝成功後,VS會自動在你的項目中添加對EntityFramework的引用。
4. 配置
EFCodeFirst的配置是相當的簡單,我們向Model中添加BlogDB類。
using System.Data.Entity;namespace Blog.Models{ public class BlogDB : DbContext { public DbSet<Post> Posts { get; set; } public DbSet<Tag> Tags { get; set; } public DbSet<Category> Categories { get; set; } public DbSet<Comment> Comments { get; set; } public DbSet<FriendLink> FriendLinks { get; set; } }}
開啟web.config檔案,添加連結字串
<connectionStrings> <add name="BlogDB" connectionString="Server=.\; Database=Blog;Trusted_Connection=true" providerName="System.Data.SqlClient" /> <!--<add name="BlogDB" connectionString="Server=.\EXPRESS; Database=Blog;Trusted_Connection=true" providerName="System.Data.SqlClient" />--></connectionStrings>
注意,name屬性的值為“BlogDB”這裡和BlogDB這個類的類名保持一致。資料庫名稱為Blog(這個資料庫現在並不存在)。
5. 小試牛刀
建立一個HomeController,添加如下代碼。
using Blog.Models;namespace Blog.Controllers{ public class HomeController : Controller { BlogDB _db = new BlogDB(); // // GET: /Home/ public ActionResult Index() { var posts = _db.Posts; return View(posts); } }}
給Index Action建立一個View,如示:
添加完後就迫不及待的果斷的奮力的按下F5吧,讓我們看看都發生了什麼!
網頁顯示了如下資訊,不過這不是今天的重點,今天的重點是資料庫。讓我們開啟資料庫看看,裡面發生了什麼。
看吧,EF自動的為我們建立了資料庫。
而且,EF足夠聰明的為我們完成了Posts到Tags的多對多聯絡!!!我們程式中並沒有和TagPosts表對應的Model,有的只是如下的兩行代碼
在Post類中
public ICollection<Tag> Tags { get; set; }
在Tag類中
public ICollection<Post> Posts { get; set; }
我們可以簡單的使用如下的代碼來獲得標籤“CSharp”中的所有文章。
var posts = _db.Tags .Where(t => t.Name == "CSharp") .Single() .Posts;
6. 修改Model後,自動更新資料表
當我們修改了Model後,運行網站時,會報錯,因為EF現在不能把更新後的Model和舊資料表對應起來。為了使資料庫隨著Model的更新而更新,我們還要做以下的工作。
開啟根目錄下的Global.asax檔案
添加如下命名空間(注意:EFCodeFirst 1.0 和 0.8 對於 DataBase 類所在的命名空間不同)
using System.Data.Entity;using Blog.Models;
建立一個BlogDBInitializer類,使他繼承DropCreateDatabaseIfModelChanges<BlogDB>,重寫Seed函數。
public class BlogDBInitializer : DropCreateDatabaseIfModelChanges<BlogDB>{ protected override void Seed(BlogDB context) { base.Seed(context); var links = new List<FriendLink> { new FriendLink{ Name="NinoFocus.com", URL=@"http://ninofocus.com", Description="NinoFocus的個人部落格" }, new FriendLink{ Name="NinoFocus at CNBlogs", URL=@"http://www.cnblogs.com/nizhuguo", Description="NinoFocus在部落格園的部落格" } }; links.ForEach(l => context.FriendLinks.Add(l)); context.SaveChanges(); }}
向Application_Start()中,添加如下代碼
每次重建資料庫後,資料庫中的資料都是被清空。而Seed()函數的作用就是向新的資料庫中添加以下初始化資料。
如上面的代碼我添加了兩個友情連結。
7. 寫在最後
小弟也是剛學EF架構,可能還有很多地方我沒注意到,或者說錯了,請大家多多指教!
源碼下載
Blog.rar
標籤: EFCodeFirst,EntityFramework,ASP.NET MVC,DDD