年前一直想寫個系列教程來完整的過一下Asp.NET MVC,同時也可以幫助一些人來避免我在工作中遇到的坑,碰過的壁。緣于能力有限,時間也不充足,一直也沒有能實現,幸好看到 Marla Sukesh 寫了個7天教程,講的挺好,就想順便翻譯過來給各位,英文水平有限,請各位多多包涵。
菜鳥,請主動動手,不段動手才會發現問題。
大神,請留下寶貴的看法。
有問題或建議盡管提。
今天先簡單用”Code First”的模式基于 EF5, MVC4 和 MVC Scaffolding(腳手架->通過Nuget安裝)創建一個簡單的圖書管理系統來熱身, 數據庫我們就選擇微軟自家的SQL Server2012了:
環境如下: Win7 with sp1 and VS2012
步驟1:創建一個以Razor為引擎的互聯網應用程序:
步驟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,那么下來自然就是要來創建Models了:
在Models的文件夾中創建三個實體類:
/// <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; } }
建三個實體類對應的Mvc View---空View就好.
然后打開Package Manager Console, 運行下面的命令:
PM> Scaffold Controller Book PM> Scaffold Controller Author PM> Scaffold Controller Publisher
如果要通過Repository訪問數據那么要在最后加上一個參數:–Repository
像:
PM> Scaffold Controller Book –Repository
如果要重新生成對應的view和Controller那么再加一個參數:-Force
像:
PM> Scaffold Controller Book –Repository –Force
然后你會得到的結果如下:
神奇吧,自動就幫你生成了通用部分的View,與數據庫交互的Repository, Controller以及比較重要的FirstMouseContext,省心省力。FirstMouseContext
我們來配置下讓自動創建的東西,顯示出來,編輯Shared/_Layout.cshtml,添加一個鏈接進去,這樣好導航到我們view里, 如:
<li>@Html.ActionLink("Books", "Index", "Books")</li> <li>@Html.ActionLink("Authors", "Index", "Authors")</li> <li>@Html.ActionLink("Publishers", "Index", "Publishers")</li>
得到了下面的錯誤信息:
我們修改外鍵為可空,重新跑一次:
PM> Scaffold Controller Book -Repository -Force PM> Scaffold Controller Author -Repository -Force PM> Scaffold Controller Publisher -Repository –Force
運行起來,又錯了:
明顯的忘記配置數據庫信息了,找到配置文件Web.config
<add name="DefaultConnection" connectionString="….”>
修改為:
<add name="FirstMouseContext" connectionString="….”>
再次啟動,正常了:
還有一點兒要捎帶注意的是 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; } }
看到解釋了吧?大概意思是說,如果你的Model的Schema有變化的時侯,如果想要重新生成數據庫,那么就應該把下面的一句加在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>()); } }
實際上你也可以放在構造函數里:
public FirstMouseContext() { System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<FirstMouse.Models.FirstMouseContext>()); }
很好很強大吧?準備好精力迎接下周的密集知識點兒轟炸吧…
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。