·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 1.创建一个EF实体数据模型

1.创建一个EF实体数据模型

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

1.创建一个EF实体数据模型

1.The Contoso University sample web application demonstrates how to create asp.net MVC 5 applications using the Entity Framework 6 and Visual Studio 2013. This tutorial uses the Code First workflow. For information about how to choose between Code First, Database First, and Model First, seeEntity Framework Development Workflows.(这个网站的例子,向我们展示了,怎样使用EF6和VS2013来创建ASP.NET MVC5应用程序。这个系列的课程使用了Code First工作流。要了解怎么在Code First,Database First 和Model First之间进行选择,请看链接这篇文章。)

2.The sample application is a web site for a fictional Contoso University. It includes functionality such as student admission, course creation, and instructor assignments. This tutorial series explains how to build the Contoso University sample application. You candownload the completed application.

(这个示例程序是为Contoso University虚构的一个网站。网站的功能包含:招生模块,课程的创建模块,布置作业模块,这个系列的课程将带你创建这个网站,你可以通过这个链接下载完整的网站源代码。)

3.课程中用到的软件环境:

  • Visual Studio 2013
  • .NET 4.5
  • Entity Framework 6 (EntityFramework 6.1.0 NuGet package)
  • Windows Azure SDK 2.2(可选)

The Contoso University Web Application

The application you'll be building in these tutorials is a simple university web site.(在这个系列课程中,你将要创建的是一个大学的简单web网站)

Users can view and update student, course, and instructor information. Here are a few of the screens you'll create.(你可以浏览,更新学生、课程信息,和老师信息,这里的一些页面,将是你将来要创建的。)

The UI style of this site has been kept close to what's generated by the built-in templates, so that the tutorial can focus mainly on how to use the Entity Framework.(这个网站的界面样式,使用的是默认提供的模板样式,所以这个课程将会把主要的精力放在,怎样去使用EF上面。PS:后面我会自己使用EasyUI和Bootstrap自己把界面美化一下。)

Create an MVC Web Application

Open Visual Studio and create a new C# Web PRoject named "ContosoUniversity".(打开VS,创建一个新的web项目,取名为ContosoUniversity)

In theNew ASP.NET Projectdialog box select theMVCtemplate. (在新的ASP.NET项目中,选择MVC模板)

If theHost in the cloudcheck box in theMicrosoft Azuresection is selected, clear it.(如果在云中托管被选中了,请取消勾选

ClickChange Authentication.(点击更改身份验证)

In theChange Authenticationdialog box, selectNo Authentication, and then clickOK. For this tutorial you won't be requiring users to log on or restricting access based on who's logged on.(在更改身份验证的对话框中,选择无身份验证,然后点击确定。这个系列的课程,你不会被要求,需要让用户来登录,或者限制用户登录)

Back in the New ASP.NET Project dialog box, clickOKto create the project. (返回到新建ASP.NET项目的界面,点击确定,创建新项目)

Set Up the Site Style(设置站点的样式)

A few simple changes will set up the site menu, layout, and home page.(我们会对菜单,样式,和主页做一些细微的改变)

OpenViews\Shared\_Layout.cshtml, and make the following changes:(打开Views\Shared\_Layout.cshtml文件,做如下改变

  • Change each occurrence of "My ASP.NET Application" and "Application name" to "Contoso University".(把每个”My ASP.NET Application“和“Application name”改成“Contoso University”)PS:这里可以随便改,你想取什么名字都行,我取武汉大学。
  • Add menu entries for Students, Courses, Instructors, and Departments, and delete the Contact entry.(为学生,课程,老师,部门添加菜单选项链接,并且删除联系方式链接)

The changes are highlighted.(这些改变都做了高亮显示:)

 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 5     <meta charset="utf-8" /> 6     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7     <title>@ViewBag.Title - WuHan University</title> 8     @Styles.Render("~/Content/CSS") 9     @Scripts.Render("~/bundles/modernizr")10 </head>11 <body>12     <div class="navbar navbar-inverse navbar-fixed-top">13         <div class="container">14             <div class="navbar-header">15                 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">16                     <span class="icon-bar"></span>17                     <span class="icon-bar"></span>18                     <span class="icon-bar"></span>19                 </button>20                 @Html.ActionLink("Wuhan University", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })21             </div>22             <div class="navbar-collapse collapse">23                 <ul class="nav navbar-nav">24                     <li>@Html.ActionLink("主页", "Index", "Home")</li>25                     <li>@Html.ActionLink("关于", "About", "Home")</li>26                     <li>@Html.ActionLink("学生", "Index", "Student")</li>27                     <li>@Html.ActionLink("课程", "Index", "Course")</li>28                     <li>@Html.ActionLink("教师", "Index", "Instructor")</li>29                     <li>@Html.ActionLink("部门", "Index", "Department")</li>30                 </ul>31             </div>32         </div>33     </div>34     <div class="container body-content">35         @RenderBody()36         <hr />37         <footer>38             <p>&copy; @DateTime.Now.Year - Wuhan University</p>39         </footer>40     </div>41 42     @Scripts.Render("~/bundles/jquery")43     @Scripts.Render("~/bundles/bootstrap")44     @RenderSection("scripts", required: false)45 </body>46 </html>

InViews\Home\Index.cshtml, replace the contents of the file with the following code to replace the text about ASP.NET and MVC with text about this application:(在Views\Home\Index.cshtml中,用下面的代码,来代替它

 1 @{ 2     ViewBag.Title = "主页"; 3 } 4  5 <div class="jumbotron"> 6     <h1>WuHan University</h1> 7 </div> 8 <div class="row"> 9     <div class="col-md-4">10         <h2>Welcome to WuHan University</h2>11         <p>12             WuHan University is a sample application that13             demonstrates how to use Entity Framework 6 in an14             ASP.NET MVC 5 web application.15         </p>16     </div>17     <div class="col-md-4">18         <h2>Build it from scratch</h2>19         <p>You can build the application by following the steps in the tutorial series on the ASP.NET site.</p>20         <p><a class="btn btn-default" href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/">See the tutorial &raquo;</a></p>21     </div>22     <div class="col-md-4">23         <h2>Download it</h2>24         <p>You can download the completed project from the Microsoft Code Gallery.</p>25         <p><a class="btn btn-default" href="http://code.msdn.microsoft.com/ASPNET-MVC-Application-b01a9fe8">Download &raquo;</a></p>26     </div>27 </div>

Press CTRL+F5 to run the site. You see the home page with the main menu.(按一下CTRL+F5,来运行项目,你可以看到:)

Install Entity Framework 6(安装EF6)

From theToolsmenu clickNuGet Package Managerand then clickPackage Manager Console.(点击工具-->NuGet程序包管理器--->程序包管理器控制台)

In thePackage Manager Consolewindow enter the following command:(在程序包管理器控制台窗口里,输入下面的指令:)

Install-Package EntityFramework

The image shows 6.0.0 being installed, but NuGet will install the latest version of Enti