EF的代码优先设计
2016-06-13 09:44
267 查看
CodeFirst 用中文说是代码优先,此技术可以让我们先写代码,然后由Entity Framework根据我们的代码建立数据库
接下来用学生这个例子来演示,有学生表,课程表,和成绩表三张表
首先是Model层
学生表
课程表
成绩表
[Key]表示在数据库中该字段为主键,[Required]表示不为空,[StringLength]也就是长度了
这一步完成之后,我们要建立一个StudentInfoEntities的类,这个类要继承自DbContext,而DbContext类在System.Data.Entity命名空间下,需引用EntityFramework.dll类库,
如安装不了,可以去Visual Studio Gallery下载,其实,只需要引用一个叫做Entity Framework的dll类库即可
StudentInfoEntities类
接着,我们在Web.Config里配置一下数据库的连接字符串
最后,新建一个HomeController
点击调试,触发一下,查看数据库
同时,您会发现,在Score表中,自动产生外键关系
接下来用学生这个例子来演示,有学生表,课程表,和成绩表三张表
首先是Model层
学生表
using System; using System.Collections.Generic; using System.Linq; using System.Web; /**/ using System.ComponentModel.DataAnnotations;//验证 namespace CodeFirstDemo.Models { public class Student { [Key] public int Id { get; set; } [Required] [StringLength(50)] public string Name { get; set; } } }
课程表
using System; using System.Collections.Generic; using System.Linq; using System.Web; /**/ using System.ComponentModel.DataAnnotations;//验证 namespace CodeFirstDemo.Models { public class Course { [Key] public int Id { get; set; } [Required] [StringLength(50)] public string Name { get; set; } } }
成绩表
using System; using System.Collections.Generic; using System.Linq; using System.Web; /**/ using System.ComponentModel.DataAnnotations;//验证 namespace CodeFirstDemo.Models { public class Score { [Key] public int Id { get; set; } public Student Student { get; set; } public Course Course { get; set; } } }
[Key]表示在数据库中该字段为主键,[Required]表示不为空,[StringLength]也就是长度了
这一步完成之后,我们要建立一个StudentInfoEntities的类,这个类要继承自DbContext,而DbContext类在System.Data.Entity命名空间下,需引用EntityFramework.dll类库,
如安装不了,可以去Visual Studio Gallery下载,其实,只需要引用一个叫做Entity Framework的dll类库即可
StudentInfoEntities类
using System; using System.Collections.Generic; using System.Linq; using System.Web; /**/ using System.Data.Entity; namespace CodeFirstDemo.Models { public class StudentInfoEntities:DbContext { public DbSet<Course> Courses { get; set; } public DbSet<Score> Scores { get; set; } public DbSet<Student> Students { get; set; } } }
接着,我们在Web.Config里配置一下数据库的连接字符串
<connectionStrings> <add name="StudentInfoEntities" connectionString="Data Source=.\SQLEXPRESS; User=test;Password=test;Initial Catalog=StudentInfo;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>
最后,新建一个HomeController
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; /**/ using CodeFirstDemo.Models; namespace CodeFirstDemo.Controllers { public class HomeController : Controller { private StudentInfoEntities db = new StudentInfoEntities(); public string Index() { var data = db.Students.ToList(); return "Database is build success!"; } } }
点击调试,触发一下,查看数据库
同时,您会发现,在Score表中,自动产生外键关系
相关文章推荐
- java jxl工具包操作excel文件
- C#中的Action<>和Func<>
- JAVA反射笔记
- java 中的单例模式
- 使用注解搭建spring mvc项目
- python 新式类和旧式类
- 用eclipse将Hadoop程序打包成jar及直接设定参数运行
- eclipse使用
- Modbus总线CRC16效验算法C语言
- (第22讲)java数组的一些编程题
- Eclipse安装Freemarker插件
- Netbeans 8.2将支持PHP7 更精彩
- SSL编程(1) 概述
- 一周stackoverflow热门问题选登:如何正确的理解python for循环中的else用法?
- C#简单判断字符编码的方法
- C语言中关于sizeof和strlen的区别
- Struts2学习笔记(6)-简单的数据校验
- Java 多线程编程
- 【Spring4揭秘】修改BeanFactory-BeanFactoryPostProcessor
- 使用Parallel.Invoke并行你的代码