天天看點

一起談.NET技術,在ASP.NET MVC3中使用EFCodeFirst 1.0

  打開VS2010,選擇 檔案>建立>項目,建立ASP.NET MVC3 Web 應用程式,我這裡把它命名為Blog。

一起談.NET技術,在ASP.NET MVC3中使用EFCodeFirst 1.0

  對于一個部落格,一下幾個類應該是必須的吧:

Post                             部落格文章類

Comment                     文章評論類,和Post是一對多的關系

Category                     目錄類,和Post是一對多的關系

Tag                             标簽類,和Post是多對多的關系

FriendLink                  友情連結類

  先不考慮管理者之類的東西。 在Model中依次添加上面的類。

一起談.NET技術,在ASP.NET MVC3中使用EFCodeFirst 1.0

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; }

}

public class Comment

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 class Category

public string Name { get; set; }

public string Description { get; set; }

public ICollection<Post> Posts { get; set; }

public class Tag

public class FriendLink

public string URL { get; set; }

  3. 添加EFCodeFirst

  選擇菜單欄的 工具 > Library Package Magager > Package Manager Console。

一起談.NET技術,在ASP.NET MVC3中使用EFCodeFirst 1.0

  在Package Manager Console中輸入以下指令安裝EFCodeFirst。

一起談.NET技術,在ASP.NET MVC3中使用EFCodeFirst 1.0

  EFCodeFirst的配置是相當的簡單,我們向Model中添加BlogDB類。

using System.Data.Entity;

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;

providerName="System.Data.SqlClient" />-->

</connectionStrings>

  注意,name屬性的值為“BlogDB”這裡和BlogDB這個類的類名保持一緻。資料庫名稱為Blog(這個資料庫現在并不存在)。

  建立一個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,如下圖示:

一起談.NET技術,在ASP.NET MVC3中使用EFCodeFirst 1.0

  添加完後就迫不及待的果斷的奮力的按下F5吧,讓我們看看都發生了什麼!

一起談.NET技術,在ASP.NET MVC3中使用EFCodeFirst 1.0

  網頁顯示了如下資訊,不過這不是今天的重點,今天的重點是資料庫。讓我們打開資料庫看看,裡面發生了什麼。

一起談.NET技術,在ASP.NET MVC3中使用EFCodeFirst 1.0

  看吧,EF自動的為我們建立了資料庫。

一起談.NET技術,在ASP.NET MVC3中使用EFCodeFirst 1.0

  而且,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 類所在的命名空間不同)

  建立一個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的個人部落格"

},

Name="NinoFocus at CNBlogs",

URL=@"http://www.cnblogs.com/nizhuguo",

Description="NinoFocus在部落格園的部落格"

};

links.ForEach(l => context.FriendLinks.Add(l));

context.SaveChanges();

  向Application_Start()中,添加如下代碼:

一起談.NET技術,在ASP.NET MVC3中使用EFCodeFirst 1.0

  每次重建資料庫後,資料庫中的資料都是被清空。而Seed()函數的作用就是向新的資料庫中添加以下初始化資料。

  如上面的代碼我添加了兩個友情連結。

  小弟也是剛學EF架構,可能還有很多地方我沒注意到,或者說錯了,請大家多多指教!