您好,登錄后才能下訂單哦!
年前一直想寫(xiě)個(gè)系列教程來(lái)完整的過(guò)一下Asp.NET MVC,同時(shí)也可以幫助一些人來(lái)避免我在工作中遇到的坑,碰過(guò)的壁。緣于能力有限,時(shí)間也不充足,一直也沒(méi)有能實(shí)現(xiàn),幸好看到 Marla Sukesh 寫(xiě)了個(gè)7天教程,講的挺好,就想順便翻譯過(guò)來(lái)給各位,英文水平有限,請(qǐng)各位多多包涵。
菜鳥(niǎo),請(qǐng)主動(dòng)動(dòng)手,不段動(dòng)手才會(huì)發(fā)現(xiàn)問(wèn)題。
大神,請(qǐng)留下寶貴的看法。
有問(wèn)題或建議盡管提。
今天先簡(jiǎn)單用”Code First”的模式基于 EF5, MVC4 和 MVC Scaffolding(腳手架->通過(guò)Nuget安裝)創(chuàng)建一個(gè)簡(jiǎn)單的圖書(shū)管理系統(tǒng)來(lái)熱身, 數(shù)據(jù)庫(kù)我們就選擇微軟自家的SQL Server2012了:
環(huán)境如下: Win7 with sp1 and VS2012
步驟1:創(chuàng)建一個(gè)以Razor為引擎的互聯(lián)網(wǎng)應(yīng)用程序:
步驟2:安裝EntityFramework
安裝EntityFramework:
PM> Install-Package EntityFramework
安裝MvcScaffolding
PM> Install-Package MvcScaffoldingAttempting to resolve dependency 'T4Scaffolding'.Attempting to resolve dependency 'T4Scaffolding.Core'.Attempting to resolve dependency 'EntityFramework'.Installing 'T4Scaffolding.Core 1.0.0'.Successfully installed 'T4Scaffolding.Core 1.0.0'.Installing 'T4Scaffolding 1.0.8'.Successfully installed 'T4Scaffolding 1.0.8'.Installing 'MvcScaffolding 1.0.9'.Successfully installed 'MvcScaffolding 1.0.9'.
既然是code first,那么下來(lái)自然就是要來(lái)創(chuàng)建Models了:
在Models的文件夾中創(chuàng)建三個(gè)實(shí)體類:
/// <summary> /// Book Entity /// </summary> public class Book { [Key] public int ID { get; set; } public string BookName { get; set; } /// <summary> /// One to One /// </summary> public int PublisherID { get; set; } [ForeignKey("PublisherID")] public virtual Publisher Publisher { get; set; } /// <summary> /// One to Many /// </summary> public virtual ICollection<Author> Authors { get; set; } } /// <summary> /// Author Entity /// </summary> public class Author { [Key] public int ID { get; set; } public string AuthorName { get; set; } public int BookID { get; set; } } /// <summary> /// Publisher Entity /// </summary> public class Publisher { [Key] public int ID { get; set; } public string PublisherName { get; set; } }
建三個(gè)實(shí)體類對(duì)應(yīng)的Mvc View---空View就好.
然后打開(kāi)Package Manager Console, 運(yùn)行下面的命令:
PM> Scaffold Controller Book PM> Scaffold Controller Author PM> Scaffold Controller Publisher
如果要通過(guò)Repository訪問(wèn)數(shù)據(jù)那么要在最后加上一個(gè)參數(shù):–Repository
像:
PM> Scaffold Controller Book –Repository
如果要重新生成對(duì)應(yīng)的view和Controller那么再加一個(gè)參數(shù):-Force
像:
PM> Scaffold Controller Book –Repository –Force
然后你會(huì)得到的結(jié)果如下:
神奇吧,自動(dòng)就幫你生成了通用部分的View,與數(shù)據(jù)庫(kù)交互的Repository, Controller以及比較重要的FirstMouseContext,省心省力。FirstMouseContext
我們來(lái)配置下讓自動(dòng)創(chuàng)建的東西,顯示出來(lái),編輯Shared/_Layout.cshtml,添加一個(gè)鏈接進(jìn)去,這樣好導(dǎo)航到我們view里, 如:
<li>@Html.ActionLink("Books", "Index", "Books")</li> <li>@Html.ActionLink("Authors", "Index", "Authors")</li> <li>@Html.ActionLink("Publishers", "Index", "Publishers")</li>
得到了下面的錯(cuò)誤信息:
我們修改外鍵為可空,重新跑一次:
PM> Scaffold Controller Book -Repository -Force PM> Scaffold Controller Author -Repository -Force PM> Scaffold Controller Publisher -Repository –Force
運(yùn)行起來(lái),又錯(cuò)了:
明顯的忘記配置數(shù)據(jù)庫(kù)信息了,找到配置文件Web.config
<add name="DefaultConnection" connectionString="….”>
修改為:
<add name="FirstMouseContext" connectionString="….”>
再次啟動(dòng),正常了:
還有一點(diǎn)兒要捎帶注意的是 DbContext:
public class FirstMouseContext : DbContext { // You can add custom code to this file. Changes will not be overwritten. // // If you want Entity Framework to drop and regenerate your database // automatically whenever you change your model schema, add the following // code to the Application_Start method in your Global.asax file. // Note: this will destroy and re-create your database with every model change. // // System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<FirstMouse.Models.FirstMouseContext>()); public DbSet<FirstMouse.Models.Book> Books { get; set; } public DbSet<FirstMouse.Models.Author> Authors { get; set; } public DbSet<FirstMouse.Models.Publisher> Publishers { get; set; } }
看到解釋了吧?大概意思是說(shuō),如果你的Model的Schema有變化的時(shí)侯,如果想要重新生成數(shù)據(jù)庫(kù),那么就應(yīng)該把下面的一句加在Global.asax文件里:
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<FirstMouse.Models.FirstMouseContext>()); } }
實(shí)際上你也可以放在構(gòu)造函數(shù)里:
public FirstMouseContext() { System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<FirstMouse.Models.FirstMouseContext>()); }
很好很強(qiáng)大吧?準(zhǔn)備好精力迎接下周的密集知識(shí)點(diǎn)兒轟炸吧…
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。