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