如果文章对您有用,请随意打赏。

您的支持将鼓励我做的更好!

不懈探索,简单生活

Entity Framework Code First 系列 2——Hello World Code first

Code First 我来了

现在我们从头开始一步一步创建一个基于Code First 模式的应用程序,看仔细了!

1. 创建类库项目,使用Nuget导入Entity framework (建议大家都是用Nuget来管理dll),或者直接引入entityframework.dll 我使用的版本是4.3

 

2. 在visual studio 新增建模项目,当然你也可以不需要,不过通过可视化建模可以通过T4模板自动生成领域模型类(需安装额外的visual studio 功能包),我们建立三个领域实体分别是学校(School) 班级(Classroom) 学生(Student)

 

 

在安装visual studio  可视化建模功能包后 点击右键可以看见成长代码的选项,自动生成的代码示例如下:

 

Student
 1 //------------------------------------------------------------------------------
 2 // <auto-generated>
 3 //     This code was generated by a tool.
 4 //     Changes to this file will be lost if the code is regenerated.
 5 // </auto-generated>
 6 //------------------------------------------------------------------------------
 7 
 8 namespace Stephen.Sample.AEF.CodeFirstSample.Domain
 9 {
10     public class Student
11     {
12         public string StudentId
13         {
14             get;
15             set;
16         }
17 
18         public string StudentName
19         {
20             get;
21             set;
22         }
23 
24         public bool StudentSex
25         {
26             get;
27             set;
28         }
29 
30         public int StudentAge
31         {
32             get;
33             set;
34         }
35 
36     }
37 }
School
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Changes to this file will be lost if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Collections.Generic;

namespace Stephen.Sample.AEF.CodeFirstSample.Domain
{
    public class School
    {
        public string SchoolId
        {
            get;
            set;
        }

        public string SchoolName
        {
            get;
            set;
        }

        public virtual List<Classroom> Classrooms
        {
            get;
            set;
        }

        public virtual Address Adrress
        {
            get;
            set;
        }

    }
}
Classroom
 1 //------------------------------------------------------------------------------
 2 // <auto-generated>
 3 //     This code was generated by a tool.
 4 //     Changes to this file will be lost if the code is regenerated.
 5 // </auto-generated>
 6 //------------------------------------------------------------------------------
 7 
 8 using System.Collections.Generic;
 9 
10 namespace Stephen.Sample.AEF.CodeFirstSample.Domain
11 {
12     public class Classroom
13     {
14         public string ClassroomId
15         {
16             get;
17             set;
18         }
19 
20         public string ClassName
21         {
22             get;
23             set;
24         }
25 
26         public virtual List<Student> Students
27         {
28             get;
29             set;
30         }
31 
32     }
33 }

3.创建DbContext

  

 

SchoolDbContext
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data.Entity;
 4 using System.Linq;
 5 using System.Text;
 6 using Stephen.Sample.AEF.CodeFirstSample.Domain.DBConfiguration;
 7 
 8 namespace Stephen.Sample.AEF.CodeFirstSample.Domain.DBContext
 9 {
10     public class SchoolDbContext : DbContext
11     {
12         public DbSet<School> Schools { get; set; }
13         public DbSet<Classroom> Classrooms { get; set; }
14         public DbSet<Student> Students { get; set; }
15 
16         protected override void OnModelCreating(DbModelBuilder modelBuilder)
17         {
18             modelBuilder.Configurations.Add(new StudentConfiguration());
19             modelBuilder.Configurations.Add(new AddressConfiguration());
20         }
21     }
22 }

 

 ok 这样一个最简单EntityFramwork Codefirst Application 就诞生了。

4. 测试

默认情况下 entityframework 会找到本地默认的SQLEXPRESS 数据实例创建数据因此可以不用配置数据库.

创建后运行即可得到初始化后的数据库表(包括主外键关系和初始数据)

Test
 1  [TestMethod]
 2         public void InitSchoolDBTestMethod()
 3         {
 4             using (var schoolDb = new SchoolDbContext())
 5             {
 6 
 7                 Database.SetInitializer(new DropCreateDatabaseIfModelChanges<SchoolDbContext>());
 8                 schoolDb.Schools.Add(MockSchool());
 9                 schoolDb.SaveChanges();
10             }
11         }

 

 

posted @ 2012-07-29 17:13  帅虫哥  阅读(2375)  评论(1编辑  收藏  举报