记录一次ef code first的学习记录

最近想做一套自己的框架,正在寻找合适的ORM,之前参照力软(很早之前的版本了)的底层代码,做了一套自己的增删改查,

但是使用起来总觉得缺了点什么?

所以决心找一个成熟的ORM框架,学习搭建,发现一下自己的不足

对了Nhibernate,Entity Framework以及SqlSugar等轻量级的框架,最后选择了Entity Framework(毕竟微软大厂,跟着大厂走)

1,首先新建一个项目,winform,mvc,控制台都可以,新建以后使用nuget按照entityframework,我目前装的是6.2的版本

2,创建model、数据库上下文类、

base("name=MyStrConn")是定义在webconfig中的数据库链接
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public enum SexType { Male, Female }

    public class Person
    {
        [Key]
        public string PersonId { get; set; }
        //姓名
        public string Name { get; set; }
        //性别
        public SexType Sex { get; set; }
        //年龄
        public int Age { get; set; }

        public Person(string personId, string name, SexType sex, int age)
        {
            PersonId = personId;
            Name = name;
            Sex = sex;
            Age = age;
        }
    }
}
using System.Data.Entity;

namespace ConsoleApp1
{
    public class MyDbContext : DbContext
    {

        public MyDbContext() : base("name=MyStrConn")
        {
        }
        public DbSet<Person> Persons { get; set; }
        }
    }
}

3.在程序包管理控制台中运行如下命令(运行前先编译下项目)

Enable-Migrations          表是启用数据库更改

Add-Migration InitialCreate    将更改提交到集合

Update-Database -Verbose   将更改同步到数据库表结构

4.这样数据库就根据我们建立的model创建了一个数据库和表Persons

5.插入一条数据到数据库表

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var db = new MyDbContext())
            {
                Person person = );
                db.Persons.Add(person);
                Student stu = );
                db.Persons.Add(stu);
                db.SaveChanges();
                Console.WriteLine("Success");

            }
            Console.ReadLine();
        }
    }
}

Entity Framework学习记录的更多相关文章

  1. Entity Framework 学习记录

    msdn  :https://msdn.microsoft.com/zh-cn/data/ee712907.aspx code first 入门: https://msdn.microsoft.com ...

  2. ADO.NET Entity Framework学习笔记(3)ObjectContext

    ADO.NET Entity Framework学习笔记(3)ObjectContext对象[转]   说明 ObjectContext提供了管理数据的功能 Context操作数据 AddObject ...

  3. MVC5 Entity Framework学习之实现主要的CRUD功能

    在上一篇文章中,我们使用Entity Framework 和SQL Server LocalDB创建了一个MVC应用程序,并使用它来存储和显示数据.在这篇文章中,你将对由 MVC框架自己主动创建的CR ...

  4. Entity Framework 学习整理

    MSDN: http://msdn.microsoft.com/en-us/data/aa937723 台湾博客: http://www.dotblogs.com.tw/yc421206/ http: ...

  5. Entity Framework 学习整理(分播客整理)

    MSDN: http://msdn.microsoft.com/en-us/data/aa937723 台湾博客: http://www.dotblogs.com.tw/yc421206/ http: ...

  6. Entity Framework 学习笔记(2)

    上期回顾:Entity Framework 学习笔记(1) Entity Framework最主要的东西,就是自己创建的.继承于DbContext的类: /// <summary> /// ...

  7. Entity Framework学习笔记

    原文地址:http://www.cnblogs.com/frankofgdc/p/3600090.html Entity Framework学习笔记——错误汇总   之前的小项目做完了,到了总结经验和 ...

  8. Entity Framework 学习中级篇1—EF支持复杂类型的实现

    本节,将介绍如何手动构造复杂类型(ComplexType)以及复杂类型的简单操作. 通常,复杂类型是指那些由几个简单的类型组合而成的类型.比如:一张Customer表,其中有FristName和Las ...

  9. MVC5 Entity Framework学习

    MVC5 Entity Framework学习(1):创建Entity Framework数据模型 MVC5 Entity Framework学习(2):实现基本的CRUD功能 MVC5 Entity ...

随机推荐

  1. 139. Word Break (String; DP)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  2. Spring配置连接池

    ---------------------siwuxie095                                 Spring 配置连接池         1.Spring 配置内置连接 ...

  3. day8:vcp考试

    Q141. An administrator is unable to upgrade a vCenter Server Appliance from version 5.1 Update 2 to ...

  4. 最近公共祖先 · Lowest Common Ancestor

    [抄题]: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. “Th ...

  5. OC 和 swift 冒泡排序

    swift 版 var numbers = [17, 28, 36, 15, 39] print("排序前\(numbers)") for i in 0..<numbers. ...

  6. html菜单和课程表

    菜单: <html> <head> <meta charset="utf-8"> <title>菜单练习</title> ...

  7. Linux 配置文件管理

    一.简介 参考:https://robots.thoughtbot.com/rcm-for-rc-files-in-dotfiles-repos http://dotfiles.github.io/ ...

  8. WinScp获取一个文件

    CD /d C:\Program Files (x86)\WinSCPWinSCP.exe /console /command "option batch continue" &q ...

  9. 电商类Web原型制作分享-IKEA

    IKEA是一个家居整合大型零售商,属于电商类官网.电商以展示商品.售后服务.购物流程为主.根据网站的图文方式排版,主导航栏使用的标签组,区域导航栏使用的是垂直选项卡,实现下拉弹出面板交互的功能. 本原 ...

  10. python函数、装饰器、迭代器、生成器

    目录: 函数补充进阶 函数对象 函数的嵌套 名称空间与作用域 闭包函数 函数之装饰器 函数之迭代器 函数之生成器 内置函数 一.函数补充进阶 1.函数对象:  函数是第一类对象,即函数可以当作数据传递 ...