ASP.NET MVC4+EF4.1系列之二实体Code First

前面我们已经能简单的建立起MVC4项目了,接下来在项目中添加一个类库,这个类库是用来建EF用的,它承担着MVC中Model层的作用。首先添加"ADO.NET实体数据模型",(我用的是简体中文版)虽然建立很简单但是照顾初学者就做了截图。

这样就完成了EF的创建

在前一篇已经说过通过一个小小的Demo去开始Code First。通过这个小例子进行简单的CRUD,首先开始创建实体。

一、创建实体

我很喜欢EF的图形化设计领域结构,实体设计如下图:

很容易的看出来学生和成绩记录以下称成绩单和学生是1对多的关系,一个学生可以有多个成绩记录,因为每个课程都会有一个成绩记录,一个课程也可以有多次记录,所以我们不难发现学生和课程是一个多对多的关系,一个学生有对个课程,当然一个课程也可以同属于多个学生。

二、写实体

关系已经理清,我们就要去写这些实体。也许用过EF的大侠们都知道EF只要你将实体设计好,后台会自动生成类。但是在这里我不这样做,我不让它自动生成,我将EF的属性,代码生成策略改成“无”,如下图所示:

大家注意画了红线的部分这样EF就不会在后台自动给我们生成代码了。那么大家会好奇,那我是不是要手动去写这些类和关系映射。答案是"NO"。这就是微软的强大了。我们用T4模板生成显示的实体代码。首先添加代码生成项

在这里选中ADO.NET DbContext Generrator,也就是图上画红线的选项,大家也许记得我在前面说我我们要用到EF4.1的一些新特性。这里开始体现了。经过以操作实体已经生成。
学生的实体:

namespace MVC4School.Model
{
    using System;
    using System.Collections.Generic;
    
    public partial class Student
    {
        public Student()
        {
            this.Performance = new HashSet<Performance>();
        }
    
        public System.Guid ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string StudentNum { get; set; }
        public System.DateTime CreateAt { get; set; }
        public string RecordTime { get; set; }
    
        public virtual ICollection<Performance> Performance { get; set; }
    }
}

 你很容易看到其中成绩记录属性被virtual修饰,说明是延迟加载,这提高了性能,在EF中这个virtual不是必须的,不用visual在引用的是后也不会报错只是为null,也许了解nhibernate的同志知道这个在nhibernate中是必须的没有是会报错的。

成绩记录的实体代码如下:

namespace MVC4School.Model
{
    using System;
    using System.Collections.Generic;
    
    public partial class Performance
    {
        public System.Guid ID { get; set; }
        public string Score { get; set; }
        public string CreatAt { get; set; }
    
        public virtual Student Student { get; set; }
        public virtual Course Course { get; set; }
    }
}

 课程实体代码如下:

namespace MVC4School.Model
{
    using System;
    using System.Collections.Generic;
    
    public partial class Course
    {
        public Course()
        {
            this.Performance = new HashSet<Performance>();
        }
    
        public System.Guid ID { get; set; }
        public string Name { get; set; }
        public string CreatAt { get; set; }
    
        public virtual ICollection<Performance> Performance { get; set; }
    }
}

 这样这三个实体的代码都完整的展现在我们的面前,但事实我们不用写一行代码,是不是很方便呢?(不要说我懒,有快捷的方式我为什么不用呢?)

三、creating the  Database Context

这个类主要将上面创建的类包含在其中,住要是指哪些实体包含在数据模型中,同时这个类指定我们创建的实体的映射关系,另一方面指定一些约束。

自动生成的代码如下:

View Code
 1 namespace MVC4School.Model
 2 {
 3     using System;
 4     using System.Data.Entity;
 5     using System.Data.Entity.Infrastructure;
 6     
 7     public partial class SchoolModelContainer : DbContext
 8     {
 9         public SchoolModelContainer()
10             : base("name=SchoolModelContainer")
11         {
12         }
13     
14         protected override void OnModelCreating(DbModelBuilder modelBuilder)
15         {
16             throw new UnintentionalCodeFirstException();
17         }
18     
19         public DbSet<Student> Students { get; set; }
20         public DbSet<Course> Courses { get; set; }
21         public DbSet<Performance> Performances { get; set; }
22     }
23 }

这里我给一些简单的解说

        public SchoolModelContainer()
            : base("name=SchoolModelContainer")
        {
        }

 "name=SchoolModelConter"这个指定了连接字符串,这个连接字符串在App.config配置文件中,你打开这个文件会看到这样一段代码:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="SchoolModelContainer" connectionString="metadata=res://*
/SchoolModel.csdl|res://*/SchoolModel.ssdl|res://*/SchoolModel.msl;
provider=System.Data.SqlClient;provider connection string='data source=.\SQLEXPRESS;
attachdbfilename="C:\Documents and
 Settings\Administrator.9682B89E07F644F\My Documents\SchoolModel.mdf";integrated security=True;connect timeout=30;

user instance=True;multipleactiveresultsets=True;App=EntityFramework'" 
providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

 这段连接字符串指定了数据库类型和连接字符串,忘了和大家说了,EF支持大部分流行数据库的。

这样T4模板为我们做了所有的代码编写工作。下一节我给大家展示数据初始化和基本的CRUD。

未完待续……

 

posted @ 2012-04-14 15:06  egojit  阅读(6930)  评论(8编辑  收藏  举报
分享按钮