zoukankan      html  css  js  c++  java
  • Entity FrameWork 操作使用详情

    Entity FrameWork 是以ADO.net为基础发展的ORM解决方案。

    一、安装Entity FrameWork框架

    二、添加ADO.Net实体数据模型

    三、EF插入数据

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EFDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                //获取数据库上下文对象
                testContext dbContext = new testContext();
                //创建数据实体
                employee emp = new employee
                {
                    name = "yangs",
                    passwd = "123",
                    age = 18
                };
                dbContext.employee.Add(emp);
                //提交数据
                dbContext.SaveChanges();
    
                Console.WriteLine(emp.id);
                Console.ReadKey();
            }
        }
    }

    四、EF删除数据

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EFDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                //获取id
                employee emp = new employee
                {
                    id = 18
                };
                //获取数据库上下文对象
                testContext dbContext = new testContext();
                //将实体添加到数据库上下文
                dbContext.employee.Attach(emp);
                //对数据删除
                dbContext.employee.Remove(emp);
                //保存
                dbContext.SaveChanges();
    
            }
        }
    }

    五、EF修改数据

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EFDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                //准备实体
                employee emp = new employee
                {
                    id = 11,
                    name = "例子",
                    passwd = "123",
                    age = 44
                };
                //获取数据库上下文对象
                testContext dbContext = new testContext();
                //将实体添加到数据库上下文
                dbContext.employee.Attach(emp);
                dbContext.Entry(emp).State = System.Data.Entity.EntityState.Modified;
                dbContext.SaveChanges();
            }
        }
    }

    六、EF 查询数据

    1.简单查询

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EFDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                testContext dbContext = new testContext();
                var list = dbContext.employee;
                foreach (var item in list)
                {
                    Console.WriteLine(item.name);
                }
                Console.ReadKey();
            }
        }
    }

    2.where 查询条件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EFDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                testContext dbContext = new testContext();
                var list = dbContext.employee.Where(p => p.id > 5);
                foreach (var item in list)
                {
                    Console.WriteLine(item.name);
                }
                Console.ReadKey();
            }
        }
    }

    2. skip(10) => 逃过10条数据,take(10) => 获取10条数据

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EFDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                testContext dbContext = new testContext();
                var list = dbContext.employee.Skip(1).Take(2);
                foreach (var item in list)
                {
                    Console.WriteLine(item.name);
                }
                Console.ReadKey();
            }
        }
    }

     3. orderBy 排序

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EFDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                testContext dbContext = new testContext();
                //降序
                var list = dbContext.employee.OrderByDescending(p => p.id);
                foreach (var item in list)
                {
                    Console.WriteLine(item.name);
                }
                Console.ReadKey();
            }
        }
    }

    4. select 查询某几个字段

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EFDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                testContext dbContext = new testContext();
                //降序
                var list = dbContext.employee.Select(p => new { p.id, p.name }).Where(p => p.id > 5);
                foreach (var item in list)
                {
                    Console.WriteLine(item.id+" -- "+item.name);
                }
                Console.ReadKey();
            }
        }
    }

    5. EF高级写法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EFDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                testContext dbContext = new testContext();
                //降序
                var list = from o in dbContext.employee where o.id > 5 select o; 
                foreach (var item in list)
                {
                    Console.WriteLine(item.id+" -- "+item.name);
                }
                Console.ReadKey();
            }
        }
    }

    join 联合查询

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EFDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                testContext dbContext = new testContext();
                //降序
                var list = from e in dbContext.employee
                           join i in dbContext.employeeInfo
                           on e.id equals i.emp_id
                           where e.id > 5 
                           select new {e, i.content};
            }
        }
    }

     七、延迟加载

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EFDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                testContext dbContext = new testContext();
                //降序
                var list = dbContext.employee.ToList();
    
                foreach (var item in list)
                {
                    
                }
            }
        }
    }

    toList()转换为本地内存数据级别,会请求数据。如果没有toList(),则当数据遍历的时候请求。

  • 相关阅读:
    在Delphi中使用indy SMTP发送gmail邮件[转]
    Delphi APP 開發入門(四)簡易手電筒
    Delphi APP 開發入門(六)Object Pascal 語法初探
    Delphi APP 開發入門(五)GPS 定位功能
    Delphi APP 開發入門(十)REST Client 開發
    Delphi APP 開發入門(九)拍照與分享
    各种电平的理解
    串口调试
    DSP中-stack和-heap的作用
    不同深度的图片转换cvConvertScale
  • 原文地址:https://www.cnblogs.com/yang-2018/p/10222140.html
Copyright © 2011-2022 走看看