zoukankan      html  css  js  c++  java
  • ORM--Entity Framework 学习(01)

    关键词:
    Entity Framework:微软官方提供的ORM工具,ORM让开发人员节省数据库访问的代码时间,将更多的时间放到业务逻辑层代码上。EF提供变更跟踪、唯一性约束、惰性加载、查询事物等。开发人员使用Linq语言,对数据库操作如同操作Object对象一样省事。
    EF有三种使用场景:1. 从数据库生成Class,2.由实体类生成数据库表结构,3.  通过数据库可视化设计器设计数据库,同时生成实体类。
    简单使用:
    定义表
        public class Assignment
        {
            public int ID { get; set; }
            public int CourseID { get; set; }
            public int InstructorID { get; set; }
            public int Year { get; set; }
            //Navigation Property,表关系
            public Semester Semester { get; set; }
            public Instructor Instructor { get; set; }
            public Course Course { get; set; }
            public ICollection<ScoreList> ScoreLists { get; set; }
            public ICollection<ForumPost> ForumPosts { get; set; }
            public ICollection<Homework> Homeworks { get; set; }
        }
    不用写额外的Annotation,ID字段自动成为主键,CourseID, InstructorID自动成为外键,值类型自动not null,空行下面的那些,EF里面叫Navigation Property,不是数据表的一部分,是多表join之后方便你直接访问join之后的部分的,表示了Entity之间的对应关系。
    定义数据库上下文(这里是简单版的)
        public class MyDataContext : DbContext
        {
            public MyDataContext(DbContextOptions<MyDataContext> options)
                : base(options)
            { }
            public DbSet<Assignment> Assignments { get; set; }
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Assignment>();
            }
        }
    EF会自动替你分析表和表之间关系,一对多,多对多,多对一,都给你自动做好,然后级联删除也会自动给你做好,假如出现了环状的级联删除路径,你也可以手动指定级联删除的行为:
    modelBuilder.Entity<Enrollment>()
        .HasOne(e=>e.Student)
        .WithMany(e=>e.Enrollments)
        .OnDelete(DeleteBehavior.Restrict);
    上面啥意思呢:一个Enrollment对应一个Student,同时一个Student对应多个Enrollments,手动建立的一对多关系,然后指定OnDelete啥都不干。
    增:
    _context.Enrollments.Add(new Models.Enrollment {
        StudentID = id,
        AssignmentID = assignmentID
    });
    _context.SaveChanges();
    删:
    _context.Enrollments.Remove(xxxxx);
    _context.SaveChanges();
    查:
    查分三步:
    1.瞬间多表join完成
    var joinedTable = _context.Applications
        .Include(a => a.Student)
        .Include(a => a.Assignment)
            .ThenInclude(a => a.Course)
        .Include(a => a.Enrollment)
            .ThenInclude(a => a.Assignment)
                .ThenInclude(a => a.Course);
    Include就是A join B,ThenInclude就是A join B join C
    2.瞬间用linq完成查询
        joinedTable.Where(e=>e.ID == id).OrderByDescending(e=>e.Time)
    3.瞬间用匿名结构把东西送出去
    var r = new
    {
        CourseName = data.Course.Title,
        Homeworks = data.Homeworks.Select(e => new
        {
            Title = e.Title,
            Description = e.Content,
            StartDate = e.StartDate,
            DueDate = e.DueDate,
        }).OrderByDescending(e=>e.StartDate),
        ForumPosts = data.ForumPosts.Select(e => new
        {
            Title = e.Title,
            Content = e.Content,
            PostTime = e.PostTime
        }).OrderByDescending(e=>e.PostTime),
        ScoreList = data.ScoreLists.Select(e => new
        {
            Title = e.Title,
        }),
    };
    改:
    直接改上面查部分中得到的实体,然后context.SaveChanges()就行了,不用构造update查询。
    从头到尾没有一处操作是用拼接字符串生成的,从头到尾所有操作都支持事务回滚。瞬间觉得EF用起来好简单啊~
    参考: http://www.cnblogs.com/xuf22/articles/5513283.html
    https://zhuanlan.zhihu.com/p/26517319

  • 相关阅读:
    LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard
    LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium
    LeetCode:151_Reverse Words in a String | 字符串中单词的逆反 | Medium
    Cellular Traffic Offloading
    在word 2010中采用EndNote X7插入引用
    屏幕截图和标记
    A Nice Paper About Mobile Data Offloading
    linux Redhat 6环境上通过源码包安装DRBD 8
    hbase shell中执行list命令报错:ERROR: org.apache.hadoop.hbase.PleaseHoldException: Master is initializing
    如何通过phoenix中查看表的主键信息
  • 原文地址:https://www.cnblogs.com/CIreland/p/6750022.html
Copyright © 2011-2022 走看看