CodeFirst 用中文说是代码优先,此技术可以让我们先写代码,然后由Entity Framework根据我们的代码建立数据库

接下来用学生这个例子来演示,有学生表,课程表,和成绩表三张表

首先是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表中,自动产生外键关系

EF的代码优先设计的更多相关文章

  1. 在快速自定义的NopCommerce中使用实体框架(EF)代码优先迁移

    我看到很多nopCommerce论坛的用户问他们如何使用Entity Framework(EF)代码优先迁移来自定义nopCommerce,添加新的字段和entites核心.我实际上在做nopComm ...

  2. C# CodeFirst(EF框架)代码优先创建数据库

    namespace WebEF.Model{ public class ModelContext:DbContext //继承DBcontext 来自EF框架 { public ModelContex ...

  3. 使用Entity Framework 4进行代码优先开发

    [原文地址]Code-First Development with Entity Framework 4   .NET 4随带发布了一个改进版的Entity Framework(EF)- 一个位于Sy ...

  4. EF之Code First代码优先

    1.前言 通过英文可知,表示的是代码优先,一般创建EF都是先创建数据库,创建根据数据库的EF实体模型,而code - first 则是反过来!... 2.代码实战 我们这次创建的不是原来的数据库EF设 ...

  5. EF框架搭建小总结--CodeFirst代码优先

    前言:之前在下总结编写了一篇 EF框架搭建小总结--ModelFirst模型优先 博文,看到一段时间内该博文的访问量蹭.蹭蹭.蹭蹭蹭...往上涨(实际也不是很多,嘿嘿),但是还是按捺不住内心的喜悦(蛮 ...

  6. EF Code First 使用 代码优先迁移(三)

    迁移到特定版本(包括降级) 到目前为止,我们一直升级到最新的迁移,但有时您可能需要升级/降级到特定的迁移. 这是目前我数据库中的表:有四个表,我降级到addEndTime这个版本(这个版本是没有gra ...

  7. 从实体框架核心开始:构建一个ASP。NET Core应用程序与Web API和代码优先开发

    下载StudentApplication.Web.zip - 599.5 KB 下载StudentApplication.API.zip - 11.5 KB 介绍 在上一篇文章中,我们了解了实体框架的 ...

  8. Atitit.提升 升级类库框架后的api代码兼容性设计指南

    Atitit.提升 升级类库框架后的api代码兼容性设计指南 1. 增加api直接增加,版本号在注释上面增加1 2. 废弃api,使用主见@dep1 3. 修改api,1 4. 修改依赖import, ...

  9. 代码优先-Code First

    非常有用的两篇文章 MSDN:Code First 迁移 博客园:CodeFirst数据迁移(不丢失数据库原有数据) EF有三种开发模式:Model First,Database First 和 Co ...

随机推荐

  1. vue实现前端导出excel表格

    1.在src目录下创建一个文件(vendor)进入Blob.js和Export2Excel.js 2.npm install -S file-saver 用来生成文件的web应用程序 3.npm in ...

  2. Mock制作假数据

    name 为属性名, rule 为规则, value 为值,属性名和生成规则之间用|分隔,生成规则的格式有7种: 字符串 String, 数字 Number, 布尔型 Boolean, 对象 Obje ...

  3. js冒泡事件

    一.什么是事件冒泡 在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处 理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么这个 ...

  4. linux oracle 查看版本

    1.查找 which sqlplus /u01/app/oracle/product/10.2.0/db_1/bin/sqlplus 可以带出 oracle 安装地址  2.登录 sqlplus 输入 ...

  5. biicode:一个现代的 C 依赖管理器

    因为经营原因,公司已经倒闭了. Biicode (just the company) post-mortemPosted on August 11, 2015 by biicode TeamThis ...

  6. MapReduce的集群行为和框架

    MapReduce的集群行为 MapReduce的集群行为包括: 1.任务调度与执行MapReduce任务由一个JobTracker和多个TaskTracker两类节点控制完成.(1)JobTrack ...

  7. Linux 系统串口信息查看

    先确认系统启动的时候串口的信息. ECM_5412@chenfl:~$ dmesg | grep tty [ 0.000000] console [tty0] enabled [ 2.511678] ...

  8. kubernetes namespace

    namespace 通常用来划分项目 默认kubectl命令 操作的namespace是default kube-system是k8s的系统组件namespaces 切换namespaces: 查看配 ...

  9. SpringBoot配置成Liunx服务

    spring boot 可以打包成可执行的脚本来启动,其原理是在打成包时,将shell脚本注入到jar包中 #参考:https://docs.spring.io/spring-boot/docs/1. ...

  10. FTP内容

    1.1.1 作业FTP部署. FTP工具或者浏览器默认使用的都是PASV模式连接FTP服务器 1.先检查有没有安装   rpm -q vsftpd 如果没有安装   yum install vsftp ...