EF框架(EF framework)

EF框架:全称EntityFramework ,它微软提供的一种ORM工具,支持面向数据的软件应用程序的技术。ORM让开发人员节省数据库访问的代码时间,将更多的时间放到业务逻辑层代码上。

ORM:ORM 是将数据存储从域对象自动映射到关系型数据库的工具。ORM主要包括3个部分:域对象、关系数据库对象、映射关系。ORM使类提供自动化CRUD,使开发人员从数据库API和SQL中解放出来。

EF框架的三种模式:

1)、DataBase First 数据库优先, 传统的表驱动方式创建EDM,然后通过EDM生成模型和数据层代码。除生成实体模型和自跟踪实现模型,还支持生成轻型DbContext。简历理解就是先设计数据库,创建好数据库映射成对象和上下文。

2)、Model First 模型优先,先创建EDM模型,再生成DDL数据库脚本和模型和数据层代码。除生成实体模型和自跟踪实现模型,支持生成轻型DbContext。简单理解就是设计EDM模型然后生成到数据库和创建对象和上下文。

3)、CodeFirst 代码优先,手动创建POCO模型,数据层DbContext及映射关系,通过Database.SetInitializer生成数据库,这种方式较灵活,但是需要手动定义实体模型和关系映射。简单理解就是自己写好实体对象和映射关系,最后通过命令生成到数据库中去。

LINQ的First与FirstOrDefault方法和Last与LastOrDefault方法。

  • First – 返回集合中的第一个元素;不延迟  
  • FirstOrDefault – 返回集合中的第一个元素(如果没有则返回默认值);不延迟  
  • Last – 返回集合中的最后一个元素;不延迟  
  • LastOrDefault – 返回集合中的最后一个元素(如果没有则返回默认值) 

         补充:Contains :判断集合中是否包含有某一元素;不延迟 

       ToList :将集合转换为List<T>集合;不延迟  

a.First与FirstOrDefault的方法,均是返回集合中的第一个元素,区别在于如果没有结果,前者(First)会有抛出异常,后者(FirstOrDefault)会返回默认值。b.Last与LastOrDefault的方法,均是返回集合中的最后一个元素,区别在于如果没有结果,前者(Last)会有抛出异常,后者(LastOrDefault)会返回默认值。

Find(id)方法

Find查找的是字符的相对位置,而不是查找值 Find函数区分大小写,与Find函数相似的函数是search find找到数据库中的字段,where根据条件进行查询,add添加数据, remove删除数据

————————

EF framework: the full name is entityframework. It is an ORM tool provided by Microsoft and supports < strong > data oriented software application technology. ORM allows developers to save code time for database access and put more time on business logic layer code.

ORM:ORM   Is a tool for automatically mapping data stores from domain objects to relational databases. ORM mainly includes three parts: domain object, relational database object and mapping relationship. ORM classes provide automated crud, freeing developers from database APIs and SQL.

Three modes of EF framework:

1)、DataBase First   Database first, the traditional table driven method creates EDM, and then generates model and data layer code through EDM. In addition to generating entity model and self tracking implementation model, it also supports the generation of lightweight dbcontext. Resume understanding is to design the database first, create a good database, and map it into objects and contexts.

2)、Model First   Model first, create EDM model first, and then generate DDL database script, model and data layer code. In addition to generating entity model and self tracking implementation model, it supports the generation of lightweight dbcontext. The simple understanding is to design the EDM model, then generate it into the database and create objects and contexts.

3)、CodeFirst   Code first, manually create poco model, data layer dbcontext and mapping relationship, and generate database through database.setinitializer. This method is flexible, but you need to manually define entity model and relationship mapping. The simple understanding is to write entity objects and mapping relationships by yourself, and finally generate them into the database through commands.

LINQ的First与FirstOrDefault方法和Last与LastOrDefault方法。

  • First  –  Returns the first element in the collection; No delay
  • FirstOrDefault  –  Returns the first element in the collection (default value if none); No delay
  • Last  –  Returns the last element in the collection; No delay
  • LastOrDefault  –  Returns the last element in the collection (default if none)

Add: contains: judge whether the set contains an element; No delay

Tolist: convert a set to list & lt; T> Set; No delay

a. Both the first and firstordefault methods return the first element in the collection. The difference is that if there is no result, the former (first) will throw an exception, and the latter (firstordefault) will return the default value. b. Both last and lastordefault methods return the last element in the collection. The difference is that if there is no result, the former (last) will throw an exception, and the latter (lastordefault) will return the default value.

Find (ID) method

Find finds the < strong > relative position of characters < / strong >, not the search value   The find function is case sensitive. A function similar to the find function is search find to find fields in the database, where to query according to conditions, add to add data, and remove to delete data