年前一直想寫個系列教程來完整的過一下Asp.NET MVC,同時也可以幫助一些人來避免我在工作中遇到的坑,碰過的壁。緣于能力有限,時間也不充足,一直也沒有能實現(xiàn),幸好看到 Marla Sukesh 寫了個7天教程,講的挺好,就想順便翻譯過來給各位,英文水平有限,請各位多多包涵。
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序制作、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了新絳免費(fèi)建站歡迎大家使用!
菜鳥,請主動動手,不段動手才會發(fā)現(xiàn)問題。
大神,請留下寶貴的看法。
有問題或建議盡管提。
今天先簡單用”Code First”的模式基于 EF5, MVC4 和 MVC Scaffolding(腳手架->通過Nuget安裝)創(chuàng)建一個簡單的圖書管理系統(tǒng)來熱身, 數(shù)據(jù)庫我們就選擇微軟自家的SQL Server2012了:
環(huán)境如下: Win7 with sp1 and VS2012
步驟1:創(chuàng)建一個以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,那么下來自然就是要來創(chuàng)建Models了:
在Models的文件夾中創(chuàng)建三個實體類:
////// Book Entity /// public class Book { [Key] public int ID { get; set; } public string BookName { get; set; } ////// One to One /// public int PublisherID { get; set; } [ForeignKey("PublisherID")] public virtual Publisher Publisher { get; set; } ////// One to Many /// public virtual ICollectionAuthors { get; set; } } /// /// Author Entity /// public class Author { [Key] public int ID { get; set; } public string AuthorName { get; set; } public int BookID { get; set; } } ////// Publisher Entity /// public class Publisher { [Key] public int ID { get; set; } public string PublisherName { get; set; } }
建三個實體類對應(yīng)的Mvc View---空View就好.
然后打開Package Manager Console, 運(yùn)行下面的命令:
PM> Scaffold Controller Book PM> Scaffold Controller Author PM> Scaffold Controller Publisher
如果要通過Repository訪問數(shù)據(jù)那么要在最后加上一個參數(shù):–Repository
像:
PM> Scaffold Controller Book –Repository
如果要重新生成對應(yīng)的view和Controller那么再加一個參數(shù):-Force
像:
PM> Scaffold Controller Book –Repository –Force
然后你會得到的結(jié)果如下:
神奇吧,自動就幫你生成了通用部分的View,與數(shù)據(jù)庫交互的Repository, Controller以及比較重要的FirstMouseContext,省心省力。FirstMouseContext
我們來配置下讓自動創(chuàng)建的東西,顯示出來,編輯Shared/_Layout.cshtml,添加一個鏈接進(jìn)去,這樣好導(dǎo)航到我們view里, 如:
得到了下面的錯誤信息:
我們修改外鍵為可空,重新跑一次:
PM> Scaffold Controller Book -Repository -Force PM> Scaffold Controller Author -Repository -Force PM> Scaffold Controller Publisher -Repository –Force
運(yùn)行起來,又錯了:
明顯的忘記配置數(shù)據(jù)庫信息了,找到配置文件Web.config
修改為:
再次啟動,正常了:
還有一點兒要捎帶注意的是 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()); public DbSet Books { get; set; } public DbSet Authors { get; set; } public DbSet Publishers { get; set; } }
看到解釋了吧?大概意思是說,如果你的Model的Schema有變化的時侯,如果想要重新生成數(shù)據(jù)庫,那么就應(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()); } }
實際上你也可以放在構(gòu)造函數(shù)里:
public FirstMouseContext() { System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges()); }
很好很強(qiáng)大吧?準(zhǔn)備好精力迎接下周的密集知識點兒轟炸吧…