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. 通过分区(Partitioning)提高Spark的运行性能

    在Sortable公司,很多数据处理的工作都是使用Spark完成的.在使用Spark的过程中他们发现了一个能够提高Sparkjob性能的一个技巧,也就是修改数据的分区数,本文将举个例子并详细地介绍如何 ...

  2. JavaHbase连接代码示例

    package com.rokid.hbase; import java.io.IOException; import org.apache.hadoop.conf.Configuration; im ...

  3. 第一个struts程序的配置过程

    然后输入project-name,比如说“test",点finish,配置web.xml,这里的org.apache.struts.action.ActionServlet就在struts- ...

  4. LeetCode: Set Matrix Zeroes 解题报告

    Set Matrix ZeroesGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it ...

  5. VBA代码分行

    如果是语句可以直接在要换行的位加一个空格一个下划: Dim MyPath As String, MyName As String, _ tmpPath As String 如果是字符串可以加以加一个空 ...

  6. 使用嵌入文档Here Documents

    Unix/Linux Shell编程实战:使用嵌入文档Here Documents 一.Here Documents(嵌入文档)Here Documents作为重定向的一种方式,指示shell从源文件 ...

  7. ThinkPHP框架快捷键使用说明

    ThinkPHP框架快捷键使用说明 php mvc框架ThinkPHP中有很多快捷键,但是很多时候我们不太明白它的意思,下面我简单的列了下他们的含义: A快速实例化Action类库 B执行行为类 C配 ...

  8. Kafka消息模拟器

    package clickstream import java.util.{Properties, Random, UUID} import kafka.producer.{KeyedMessage, ...

  9. CenOS6.5下源码安装vim-7.4

    1.[下载] vim-7.4下载地址: ftp://ftp.vim.org/pub/vim/unix/vim-7.4.tar.bz2 2.[解压] tar jxvf vim-7.4.tar.bz2 之 ...

  10. springMVC demo搭建

    1.使用idea新建一个基于maven的web项目,参考 http://www.cnblogs.com/winkey4986/p/5279820.html 2.采取了比较偷懒的配置方法,只配置了一个D ...