一个简单MVC5 + EF6示例分享
本文所使用的软件及环境:
visual studio ultimate 2013;
mvc5 + ef6 + .net framework 4.5 + localdb;windows 7 x64 professional
说明:
1.在ef (entity framework,以下简称ef6)框架下,操作数据的方式有三种:database first, model first, 以及 code first,本文基于code first创建。
2.本文是基于mvc5创建:
3.localdb
- localdb是sql server express数据库引擎的轻量级版本,其非常易于安装、配置、以命令行启动并运行在user model.
- localdb以一种sql server express特殊的执行模型运行,从而使得你能够以.mdf文件的方式来操作数据库。如果你想使得数据库具有随项目迁移的能力,你可以把localdb数据库文件放在web项目的app_data文件夹下。
- 在sql server express中虽然你能够通过使用用户示例功能来达到操作.mdf文件的目的,但是这种做法是不推荐的,相反,localdb是被推荐的方式。在visual studio2012及随后的版本中,localdb随visual studio一起默认安装的。
- 通常来说sql server express并不会被用于web应用程序的生产环境,同样地,localdb由于其并不是针对iis而设计的也不被推荐使用于生产环境。
一、创建基于mvcweb application
在正式开始之前,先看一下vs 2013的启动界面,是不是有点冷酷的感觉
好了,言归正传,首先按如下截图创建
创建完成后,我们对网站的风格做些微调,以便能契合应用主题
views\shared\_layout.cshtml做如下更改(请看黄色高亮部分)
<!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 替换成如下内容 @{ 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" href="http://www.cnblogs.com/panchunting/p/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application.html">see the tutorial »</a></p> </div> </div>
运行看一下效果吧
安装ef6
创建数据模型
在models文件夹下,分别创建contact.cs、enrollment.cs、group.cs三个类
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:发现vs 2013有一个自动提示reference,是不是很方便啊
创建database context
在pct.contact项目下新建文件夹dal(data access layer),继而继续新建communicationcontext.cs
悲剧啊,由于类contact和项目名称contact重复,不得不写全称啊,以后注意。
继续在dal目录下创建communicationinitializer.cs
为了通知ef使用你创建的initializer class,在项目的web.config中添加entityframework节点
<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>
在项目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>
运行结果
查看localdb
希望本文可以对大家学习有所帮助。