首页 技术 正文
技术 2022年11月8日
0 收藏 954 点赞 2,105 浏览 2082 个字

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

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

首先是Model层

学生表

EF的代码优先设计

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; }
}
}

EF的代码优先设计

课程表

EF的代码优先设计

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; }
}
}

EF的代码优先设计

成绩表

EF的代码优先设计

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; } }
}

EF的代码优先设计

[Key]表示在数据库中该字段为主键,[Required]表示不为空,[StringLength]也就是长度了

这一步完成之后,我们要建立一个StudentInfoEntities的类,这个类要继承自DbContext,而DbContext类在System.Data.Entity命名空间下,需引用EntityFramework.dll类库,

如安装不了,可以去Visual Studio Gallery下载,其实,只需要引用一个叫做Entity Framework的dll类库即可

StudentInfoEntities类

EF的代码优先设计

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; }
}
}

EF的代码优先设计

接着,我们在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

EF的代码优先设计

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!";
} }
}

EF的代码优先设计

点击调试,触发一下,查看数据库

EF的代码优先设计

同时,您会发现,在Score表中,自动产生外键关系

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774