设为首页 加入收藏

TOP

Entity Framework学习之创建Entity Framework数据模型(一)
2015-07-20 17:52:13 来源: 作者: 【 】 浏览:6
Tags:Entity Framework 习之 创建 数据 模型

1.准备

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

2.Contoso University web应用程序

此应用程序允许用户 查看、更新student、 course和instructor 信息:

\

网站UI样式是由内置模板生成的

3.新建MVC5 web应用程序

新建项目并命名为“ContosoUniversity”

\

选择MVC模板

\

更改身份认证方式为“No Authentication”

\

4.设置网站样式

打开Views\Shared\_Layout.cshtml并作以下更改,将出现的每一个"My ASP.NET Application" 和"Application name" 改为 "Contoso University",添加 Students, Courses, Instructor,和Departments菜单项,代码如下:



    
  
    
  
    @ViewBag.Title - Contoso University
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")


    
  
    
  
@RenderBody()

© @DateTime.Now.Year - Contoso University

@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false)

打开Views\Home\Index.cshtml,使用下面的代码替换掉原来的内容:

@{
    ViewBag.Title = "Home Page";
}


  

Contoso University

Welcome to Contoso University

Contoso University is a sample application that demonstrates how to use Entity Framework 6 in an ASP.NET MVC 5 web application.

Build it from scratch

You can build the application by following the steps in the tutorial series on the ASP.NET site.

See the tutorial ?

Download it

You can download the completed project from the Microsoft Code Gallery.

Download ?


按CTRL+F5 运行程序:

\

\

改变浏览器窗口大小,可以发现内容可以自适应窗口

\

\

5.安装Entity Framework 6

依次打开“Tools”->“Library Package Manager”->"Package Manager Console",在”Package Manager Console“中输入Install-Package EntityFramework命令后回车

\

\

使用手动添加Entity Framework的好处是可以明白接下来如何使用Entity Framework。
也可以使用另一种比较简单的方法添加Entity Framework:项目点右键选择“Manage NuGet Packages”,搜索“EntityFramework”:

\\

6.创建数据模型

现在要创建下面三个实体Student和Enrollment之间是一对多的关系,Course和Enrollment也是一对多的关系。

\\

注意:如果在创建完这三个实体类后尝试编译项目,会出现编译错误.


Student实体

\\


在Models目录下新建Student.cs类,添加代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ContosoUniversity.Models
{
    public class Student
    {
            public int ID { get; set; }
            public string LastName { get; set; }
            public string FirstMidName { get; set; }
            public DateTime EnrollmentDate { get; set; }
            public virtual ICollection
  
    Enrollments { get; set; }
        
    }
}

  

其中属性ID相当于数据库表中的primary key。默认情况下,Entity Framework将ID或者classnameID属性作为primary key。

属性Enrollments是一个导航属性(navigation property),导航属性可以将其他实体和此实体关联起来。本例中,Student实体中的Enrollments属性包含所有与Student实体相关联的Enrollments实体。换句话说,如果数据库中给定一条Student记录,此记录有两条相关联的Enrollment 记录,那么Student实体中的Enrollments导航属性就包含这两个两个Enrollment实体。

导航属性一般被定义为virtual类型,这样可以方便使用Entity Framework框架的一些特性,比如延迟加载( lazy loading)。

如果导航属性可包含多个实体(如多对多或者一对多的关系),那么导航属性的类型必须是一个可进行添加、删除、更新操作的列表类型比如ICollection。


Enrollment实体

\

\

在Models目录下新建Enrollment.cs类,添加代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ContosoUniversity.Models
{
    public class Enrollment
    {
        public enum Grade
        {
            A, B, C, D, F
        }

        public class Enrollment
        {
            public
首页 上一页 1 2 3 4 5 6 下一页 尾页 1/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ 1840 Eqs(hash) 下一篇hdu 4960 Another OCD Patient(记..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: