Lv.的博客

EF的代码优先设计

 

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

 

 

 

 

 

 

posted @   Avatarx  阅读(1053)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
历史上的今天:
2014-06-13 Linux(Debian) vps安装gnome桌面+VNC
2014-06-13 debian下Vnc
2014-06-13 oracle 直接客户端使用
2014-06-13 不安装oracle客户端也可以使用pl/sql developer
2014-06-13 JDK 7u60 版本发布下载安装
点击右上角即可分享
微信分享提示