1 创建各个实体类

2 创建一个空数据模型,然后删除掉,为了引入Entity Framework和System.Data.Entity

3 为实体类增加标注

4 为实体增加导航属性

5 在App.config configuration标签里面增加connectionStrings标签,并增加连接字符串

6 创建数据操作类继承DbContext ,构造函数参数为“name=connStr”

7 增加两个DbSet属性,对应操作的实体

8 重写OnModelCreating方法,使得生成的表不包含复数;

9 调用context.Database.CreateIfNotExists()在对应的数据库下面生成表

10 通过数据操作类操作表

//ClassInfo

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeFirstDemo
{
    public class ClassInfo
    {
        [Key]
        public int Id { get; set; }
        [Required]
        [StringLength(32)]
        public string ClassName { get; set; }
        [Required]
        public DateTime CreateTime { get; set; }
        public ICollection<StudentInfo> StudentInfo { get; set; }
    }

}

//StudentInfo

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeFirstDemo
{
    public class StudentInfo
    {
        [Key]
        public int Id { get; set; }
        [Required]
        [StringLength(32)]
        public string StudentName { get; set; }
        [Required]
        public DateTime CreateTime { get; set; }
        [Required]
        public ClassInfo ClassInfo { get; set; }
    }

}

//MyDataContext

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeFirstDemo
{
    public class MyDataContext:DbContext
    {
        public MyDataContext()
            : base("name = ConnStr")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

        public DbSet<ClassInfo> ClassInfo { get; set; }
        public DbSet<StudentInfo> StudentInfo { get; set; }

    }

}

//主方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeFirstDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            MyDataContext db = new MyDataContext();
            db.Database.CreateIfNotExists();
            ClassInfo classInfo = new ClassInfo();
            classInfo.ClassName = "abc";
            classInfo.CreateTime = DateTime.Now;
            db.ClassInfo.Add(classInfo);

            StudentInfo stu1 = new StudentInfo();
            stu1.StudentName = "zhangsan";
            stu1.CreateTime = DateTime.Now;
            stu1.ClassInfo = classInfo;
            StudentInfo stu2 = new StudentInfo();
            stu2.StudentName = "Lisi";
            stu2.CreateTime = DateTime.Now;
            stu2.ClassInfo = classInfo;
            db.StudentInfo.Add(stu1);
            db.StudentInfo.Add(stu2);
            db.SaveChanges();
        }
    }
}

EF CodeFirst的步骤的更多相关文章

  1. EF CodeFirst 命令步骤

    添加EntityFramework 命令:Install-Package EntityFramework 1.启用迁移 Enable-Migrations 2.为挂起的Model变化添加迁移脚本 Ad ...

  2. [.NET领域驱动设计实战系列]专题一:前期准备之EF CodeFirst

    一.前言 从去年已经接触领域驱动设计(Domain-Driven Design)了,当时就想自己搭建一个DDD框架,所以当时看了很多DDD方面的书,例如领域驱动模式与实战,领域驱动设计:软件核心复杂性 ...

  3. EF Codefirst 初步学习(二)—— 程序管理命令 更新数据库

    前提:搭建成功codefirst相关代码,参见EF Codefirst  初步学习(一)--设置codefirst开发模式 具体需要注意点如下: 1.确保实体类库程序生成成功 2.确保实体表类库不缺少 ...

  4. 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...

  5. [转]Using Entity Framework (EF) Code-First Migrations in nopCommerce for Fast Customizations

    本文转自:https://www.pronopcommerce.com/using-entity-framework-ef-code-first-migrations-in-nopcommerce-f ...

  6. EF CodeFirst 如何通过配置自动创建数据库<当模型改变时>

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精    本篇为进阶篇,也是弥补自己之前没搞明白的地方,惭愧 ...

  7. EF CodeFirst增删改查之‘CRUD’

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精    本篇旨在学习EF增删改查四大操作 上一节讲述了EF ...

  8. EF CodeFirst 创建数据库

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精    话说EF支持三种模式:Code First   M ...

  9. 新年奉献MVC+EF(CodeFirst)+Easyui医药MIS系统

    本人闲来无事就把以前用Asp.net做过的一个医药管理信息系统用mvc,ef ,easyui重新做了一下,业务逻辑简化了许多,旨在加深对mvc,ef(codefirst),easyui,AutoMap ...

随机推荐

  1. event.relatedTarget、event.fromElement、event.toElement

    在标准DOM中,mouseover和mouseout所发生的元素可以通过event.target来访问,相关元素通过event.relatedTarget属性来访问.event.relatedTarg ...

  2. Windows Phone 8.1 联系人与日历

    (1)联系人(Manifest 获取权限) 1)获取联系人 获取联系人的方式有两种 A. ContactPicker ContactPicker 也就是直接打开一个系统的选择联系人界面,让用户选择,可 ...

  3. 【33.20%】【LA 4320】【Ping pong】

    [Description] N (3 ≤ N ≤ 20000) ping pong players live along a west-east street(consider the street ...

  4. arm-linux内存管理学习笔记(1)-内存页表的硬件原理

    linux kernel集中了世界顶尖程序猿们的编程智慧,犹记操作系统课上老师讲操作系统的四大功能:进程调度 内存管理 设备驱动 网络.从事嵌入式软件开发工作,对设备驱动和网络接触的比較多. 而进程调 ...

  5. js如何实现点击显示和隐藏表格

    js如何实现点击显示和隐藏表格 一.总结 一句话总结: 1.给table或者table里面的元素添加点击事件, 2.然后判断当前表格的数据显示或者隐藏, 3.然后通过display属性显示(非none ...

  6. 编辑框等控件边框美化(继承CEdit,然后覆盖OnMouseLeave, OnSetFocus, OnPaint函数即可。原来的CEdit虽然代码不可见,但它也是有句柄的,照样随便画)

    源码说明:美化能获取焦点控件的边框颜色,获取焦点后颜色不同(类似彗星小助手.QQ等软件),支持自定义颜色,支持单独设置各个控件颜色.实现方法:子类化,在WM_NCPAINT.WM_PAINT等消息自己 ...

  7. ES6与React中this完全解惑

    计划写很长的篇幅,预计12月初完成. 这篇文章涉及的知识较多,可能一次消化不了,可以渐渐来. 先说结论: 无论是ES6还是React的this,相对于ES5,只是增加了箭头函数this绑定了其封闭上下 ...

  8. 单点登录原理与简单实现--good

    一.单系统登录机制 1.http无状态协议 web应用采用browser/server架构,http作为通信协议.http是无状态协议,浏览器的每一次请求,服务器会独立处理,不与之前或之后的请求产生关 ...

  9. 至Linux-2.6.32编译内核ipset-6.23坎坷的经历

    新的版本号ipset 上周,一名医生在儿童医院等待一段差距叫做数量.接受NetfilterPush信息的邮件列表,列表ipset最新6.23版本号的新功能,非常喜欢我现在需要的是,特别是timeout ...

  10. Qt自定义弹窗屏蔽父窗口(QWidget设置setWindowModality(Qt::ApplicationModal);以后再show)

    写Qt程序时遇到一个问题: Qt自带的弹窗功能单一,所以须要自己用ui设计弹窗的内容,这样弹窗就和普通窗口一样了,但问题是这个弹窗显示后父窗口还是活动的.网上找了很久找到了解决办法: Qt::Wind ...