Entity Framework 4.1支持代碼優(yōu)先(code first)編程模式:即可以先創(chuàng)建模型類,然后通過配置在EF4.1下動態(tài)生成數(shù)據(jù)庫。
下面演示兩種情形:
1、代碼優(yōu)先模式下,asp.net mvc數(shù)據(jù)訪問
2、傳統(tǒng)模式,先創(chuàng)建數(shù)據(jù)庫和表,配置連接字符串,再生成模型
第一種情況的步驟:
(1)使用空模板,創(chuàng)建ASP.NET MVC3.0(或4.0)項(xiàng)目,假定項(xiàng)目名:MVC_Student
注意:創(chuàng)建完項(xiàng)目后,項(xiàng)目會自動引用EF4.1
(2)在Model文件夾下,創(chuàng)建數(shù)據(jù)庫上下文類:StuDBContext
public class StuDBContext:DbContext { public StuDBContext() :base("DataConn") { }
public DbSet<StudentInfo> Students { get; set; } }
(3)創(chuàng)建域模型:StudentInfo
public classStudentInfo { public int ID { get; set; } public string StuNO { get; set; } public string StuName { get; set; } public string StuPhoto { get; set; } public DateTime StuBirthday { get; set; } public string StuAddress { get; set; } }
(4)在web.config中配置連接字符串(也可以不配置,EF自動檢查并使用SQL SERVER EXPRESS,此處我們指定服務(wù)器和數(shù)據(jù)庫)
<connectionStrings><!--<add name="StuDBContext" connectionString="server=(local);database=MyStudent;uid=(登錄賬戶);pwd=(登錄密碼)" providerName="System.Data.SqlClient"/>--><add name="DataConn" connectionString="server=(local);database=MyStudent;uid=(登錄賬戶);pwd=(登錄密碼)" providerName="System.Data.SqlClient"/></connectionStrings>
(5)生成項(xiàng)目,為第(6)步服務(wù)
(6)右擊“Controllers"文件夾,選擇”添加控制器“,如圖:

單擊確定后,會在Controllers文件夾下生成一個StudentController類,而且在Views文件夾下生成Student子文件夾,其中包含5個.cshtml文件,如圖:

(7)修改Global.asax.cs的默認(rèn)路由:
routes.MapRoute( "Default", // 路由名稱 "{controller}/{action}/{id}", // 帶有參數(shù)的 URL new { controller = "Student", action = "Index", id = UrlParameter.Optional } // 參數(shù)默認(rèn)值 );
(8)最后,單擊”調(diào)試“菜單,選擇”啟動調(diào)試“,或直接按F5.會看到如下效果:

(9)可以單擊”Create New"超鏈接,向數(shù)據(jù)庫添加一條記錄
(10)此時可以打開數(shù)據(jù)庫服務(wù)器,會發(fā)現(xiàn)自動創(chuàng)建了MyStudent的數(shù)據(jù)庫(對應(yīng)連接字符串中的數(shù)據(jù)庫)和StudentInfoes表(是模型類名稱的復(fù)數(shù)形式,表中的各字段分別對應(yīng)模型類中的屬性,此處要特別注意:ID屬性會自動對應(yīng)表中的自增長主鍵列。
以上方法需要注意的地方:
(1)web.config中連接字符串,providerName要提供,否則報錯。
(2)模型的ID屬性是固定的, 要不就要用元數(shù)據(jù)聲明,EF4.1會自動將之映射為表的主鍵(自增長)。
(3)數(shù)據(jù)庫實(shí)體上下文名稱一般與連接字符串name屬性的值相同,但本文中不同(數(shù)據(jù)庫實(shí)體上下文是StuDBContext,連接字符串名稱:DataConn),如果相同,那么實(shí)體上下文類可以不提供構(gòu)造函數(shù)。如果不相同,如本例中,可以為實(shí)體上下文添加構(gòu)造函數(shù),并調(diào)用父類構(gòu)造函數(shù),在base()中傳遞與實(shí)體上下文類名不同的連接字符串名稱(本例中時DataConn,如步驟(2)紅色標(biāo)注)
________________________________________________________________________________________________________________________________________
下面演示第2種情形:先創(chuàng)建數(shù)據(jù)庫模式,然后生成模型
(1)在SQL SERVER 2005/2008服務(wù)器上創(chuàng)建數(shù)據(jù)庫MyStudent,并添加一張表StudentInfoes(也可以在VS中服務(wù)器資源管理器中操作)
表的結(jié)構(gòu)如下;

(2)創(chuàng)建ASP.NET MVC3/4項(xiàng)目(使用空模板)
(3)在Models文件夾中添加實(shí)體上下文類:StuDBContext
public class StuDBContext:DbContext { public StuDBContext() : base("DataConn") { }
public DbSet<StudentInfo> Students { get; set; } }
(4)在Models文件夾中添加實(shí)體類:StudentInfo
public class StudentInfo { public int ID { get; set; } public string StuNO { get; set; } public string StuName { get; set; } public string StuPhoto { get; set; } public DateTime StuBirthday { get; set; } public string StuAddress { get; set; } }
(5)在web.config中配置連接字符串:
<connectionStrings><!--<add name="StuDBContext" connectionString="server=(local);database=MyStudent;uid=(登錄賬戶);pwd=(登錄密碼)" providerName="System.Data.SqlClient"/>--><add name="DataConn" connectionString="server=(local);database=MyStudent;uid=(登錄賬戶);pwd=(登錄密碼)" providerName="System.Data.SqlClient"/></connectionStrings>
(6)生成項(xiàng)目解決方案,為第(7)步服務(wù)
(7)在Controllers文件夾下添加StudentController類,如圖:

單擊添加后,會創(chuàng)建與第一種情形同樣的項(xiàng)目文件結(jié)構(gòu)。如上面所示。
(8)修改路由:
routes.MapRoute( "Default", // 路由名稱 "{controller}/{action}/{id}", // 帶有參數(shù)的 URL new { controller = "Student", action = "Index", id = UrlParameter.Optional } // 參數(shù)默認(rèn)值 );
(9)啟動調(diào)試,預(yù)覽效果如下:

對比兩種方式,有如下區(qū)別:自動生成數(shù)據(jù)庫表的字段類型可能與自定義的不一致。
無論是通過代碼生成數(shù)據(jù)庫,還是先創(chuàng)建數(shù)據(jù)庫,再創(chuàng)建模型,都需要了解他們之間的映射關(guān)系(即EF作為ORM框架的作用),你會發(fā)現(xiàn)EF4.1作為ORM框架,遵循很多約定(Convention),而ASP.NET MVC推崇的很重要的一條原則就是:約定先于配置,可謂不謀而合。
轉(zhuǎn)載自:http://blog.csdn.net/sdtsfhh/article/details/8141242
新聞熱點(diǎn)
疑難解答
圖片精選