一個(gè)簡(jiǎn)單MVC5 + EF6示例分享
本文所使用的軟件及環(huán)境:
Visual Studio Ultimate 2013;
MVC5 + EF6 + .NET Framework 4.5 + LocalDB;Windows 7 x64 Professional
說(shuō)明:
1.在EF (Entity Framework,以下簡(jiǎn)稱EF6)框架下,操作數(shù)據(jù)的方式有三種:Database First, Model First, 以及 Code First,本文基于Code First創(chuàng)建。
2.本文是基于MVC5創(chuàng)建:
3.LocalDB
- LocalDB是SQL Server Express數(shù)據(jù)庫(kù)引擎的輕量級(jí)版本,其非常易于安裝、配置、以命令行啟動(dòng)并運(yùn)行在user model.
- LocalDB以一種SQL Server Express特殊的執(zhí)行模型運(yùn)行,從而使得你能夠以.mdf文件的方式來(lái)操作數(shù)據(jù)庫(kù)。如果你想使得數(shù)據(jù)庫(kù)具有隨項(xiàng)目遷移的能力,你可以把LocalDB數(shù)據(jù)庫(kù)文件放在web項(xiàng)目的App_Data文件夾下。
- 在SQL Server Express中雖然你能夠通過(guò)使用用戶示例功能來(lái)達(dá)到操作.mdf文件的目的,但是這種做法是不推薦的,相反,LocalDB是被推薦的方式。在Visual Studio2012及隨后的版本中,LocalDB隨Visual Studio一起默認(rèn)安裝的。
- 通常來(lái)說(shuō)SQL Server Express并不會(huì)被用于Web應(yīng)用程序的生產(chǎn)環(huán)境,同樣地,LocalDB由于其并不是針對(duì)IIS而設(shè)計(jì)的也不被推薦使用于生產(chǎn)環(huán)境。
一、創(chuàng)建基于MVCWeb Application
在正式開(kāi)始之前,先看一下VS 2013的啟動(dòng)界面,是不是有點(diǎn)冷酷的感覺(jué)
好了,言歸正傳,首先按如下截圖創(chuàng)建
創(chuàng)建完成后,我們對(duì)網(wǎng)站的風(fēng)格做些微調(diào),以便能契合應(yīng)用主題
Views\Shared\_Layout.cshtml做如下更改(請(qǐng)看黃色高亮部分)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>@ViewBag.Title - Contact</title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") </head> <body> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> @Html.ActionLink("Contact", "Index", "Home", null, new { @class = "navbar-brand" }) </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li>@Html.ActionLink("Home", "Index", "Home")</li> <li>@Html.ActionLink("About", "About", "Home")</li> <li>@Html.ActionLink("Contacts", "Index", "Contact")</li> <li>@Html.ActionLink("Groups", "Index", "Group")</li> </ul> </div> </div> </div> <div class="container body-content"> @RenderBody() <hr /> <footer> <p>© @DateTime.Now.Year - Contact</p> </footer> </div> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false) </body> </html> Views\Home\Index.cshtml 替換成如下內(nèi)容 @{ ViewBag.Title = "Home Page"; } <div class="jumbotron"> <h1>Contact</h1> </div> <div class="row"> <div class="col-md-4"> <h2>Welcome to Contact</h2> <p> Contact is a sample application that demonstrates how to use Entity Framework 6 in an ASP.NET MVC 5 web application. </p> </div> <div class="col-md-4"> <h2>Build it from scratch</h2> <p>You can build the application by following the steps in the tutorial series on the following site.</p> <p><a class="btn btn-default" >See the tutorial »</a></p> </div> </div>
運(yùn)行看一下效果吧
安裝EF6
創(chuàng)建數(shù)據(jù)模型
在Models文件夾下,分別創(chuàng)建Contact.cs、Enrollment.cs、Group.cs三個(gè)類
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace PCT.Contact.Models { public class Contact { public int ID { get; set; } public string Name { get; set; } public DateTime EnrollmentDate { get; set; } public virtual ICollection<Enrollment> Enrollments { get; set; } } } using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace PCT.Contact.Models { public class Enrollment { public int EnrollmentID { get; set; } public int ContactID { get; set; } public int GroupID { get; set; } public virtual Contact Contact { get; set; } public virtual Group Group { get; set; } } } using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace PCT.Contact.Models { public enum GroupName { Friend, Family, Colleague, Schoolmate, Stranger } public class Group { public int GroupID { get; set; } public GroupName? GroupName { get; set; } public virtual ICollection<Enrollment> Enrollments { get; set; } } }
PS:發(fā)現(xiàn)VS 2013有一個(gè)自動(dòng)提示reference,是不是很方便啊
創(chuàng)建Database Context
在PCT.Contact項(xiàng)目下新建文件夾DAL(Data Access Layer),繼而繼續(xù)新建CommunicationContext.cs
悲劇啊,由于類Contact和項(xiàng)目名稱Contact重復(fù),不得不寫(xiě)全稱啊,以后注意。
繼續(xù)在DAL目錄下創(chuàng)建CommunicationInitializer.cs
為了通知EF使用你創(chuàng)建的initializer class,在項(xiàng)目的web.config中添加entityFramework節(jié)點(diǎn)
<entityFramework> <contexts> <context type="PCT.Contact.DAL.CommunicationContext, PCT.Contact"> <databaseInitializer type="PCT.Contact.DAL.CommunicationInitializer, PCT.Contact" /> </context> </contexts> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework>
在項(xiàng)目web.config中添加connectionstrings(在appSettings之上)
<connectionStrings> <add name="CommunicationContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContactCommunication;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/> </connectionStrings> <appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings>
運(yùn)行結(jié)果
查看LocalDB
希望本文可以對(duì)大家學(xué)習(xí)有所幫助。
相關(guān)文章
此頁(yè)的狀態(tài)信息無(wú)效,可能已損壞 的處理辦法及原因分析
此頁(yè)的狀態(tài)信息無(wú)效,可能已損壞 的處理辦法及原因分析,需要的朋友可以參考一下2013-06-06Asp.net頁(yè)面中調(diào)用soapheader進(jìn)行驗(yàn)證的操作步驟
這篇文章主要介紹了Asp.net頁(yè)面中調(diào)用soapheader進(jìn)行驗(yàn)證的操作步驟,感興趣的小伙伴們可以參考一下2016-04-04Asp.net MVC使用swupload實(shí)現(xiàn)多圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了Asp.net MVC使用swupload實(shí)現(xiàn)多圖片上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07asp.net DbProviderFactory的使用-示例
NET 2.0有一個(gè)抽象工廠模式的典型應(yīng)用:通過(guò)DBProviderFactory 可以對(duì)不同數(shù)據(jù)庫(kù)進(jìn)行操作。2009-11-11在ASP.NET Core中顯示自定義的錯(cuò)誤頁(yè)面
大家在用瀏覽器訪問(wèn)服務(wù)器時(shí),不同情況下會(huì)返回不同的信息。服務(wù)器發(fā)生錯(cuò)誤就會(huì)返回錯(cuò)誤信息,我們最熟悉的就是404錯(cuò)誤頁(yè)面,但是這里我想和大家分享下在ASP.NET Core中如何顯示自定義的500或404錯(cuò)誤頁(yè)面,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-12-12Repeater控件實(shí)現(xiàn)編輯、更新、刪除等操作示例代碼
如何在Repeater控件中實(shí)現(xiàn)像GridView控件一樣的編輯、更新、刪除功能?下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下,希望對(duì)大家有所幫助2014-01-01ASP.NET(C#)驗(yàn)證數(shù)字的兩種方法
ASP.NET(C#)驗(yàn)證數(shù)字的兩種方法,需要的朋友可以參考一下2013-06-06一天精通asp.net的學(xué)習(xí)經(jīng)驗(yàn)小結(jié)
一天精通asp.net的學(xué)習(xí)經(jīng)驗(yàn)小結(jié)2010-02-02Asp.net SignalR創(chuàng)建實(shí)時(shí)聊天應(yīng)用程序
這篇文章主要介紹了Asp.net SignalR創(chuàng)建實(shí)時(shí)聊天應(yīng)用程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11