MVC Entity Framework (data persistence framework) EntityFrameWork (EF)

Source: Internet
Author: User

EF-EntityFrameWork

Chinese name: Entity Framework (data persistence Framework)

 

1. Use EF to query (Linq to EF)

1.1 Use standard query operators to query

OumindBlogEntities db = new OumindBlogEntities();IQueryable<BlogArticle> list = db.BlogArticles.Where(a => a.ACate==2);

 

1.2 use Linq to EF

// 1. EF is used by defaultDelayed Loading

// 1.1 IQueryable <T> supports latency recording. When compiled into SQO by linq, an expression tree is generated during runtime. That is to say, IQueryable does not save the query data, instead, it stores the Expression Tree of the Data conditions to be queried.

IQueryable<BlogArticle> list = from a in db.BlogArticles where a.ACate == 2 select a;

 

// 1.2 When IQueryable is used (ToList, foreach traversal), an SQL statement is generated using the conditions in the Expression Tree and queried in the database.

List. ToList (). ForEach (a => Console. WriteLine (a. AId + "," + a. ATitle ));

 

2. Expression

// 1. Create an expression object

Expression<Func<string, bool>> pre = (str => str.Length > 0);

 

// 2. Compile the expression tree to generate the specified delegate object

Func<string,bool> fu = pre.Compile();

 

// 3. Run the delegate

fu("123");

 

3. about setting the default value of the Parameter

public List<T> GetPagedList<TKey>(int pageIndex, int pageSize, out int rowCount, out int pageCount, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> orderby, bool isAsc = true)

 

4. In Assembly U (such as the UI Layer), if Class B (such as BLL layer) in assembly B is used ):

4.1 If Class B directly creates class objects in assembly D (such as the DAL layer), there is no problem.

4.2 if Class B is defined and the class of assembly D is used (such as inheritance and generic parameters), Assembly U requires that the reference of Assembly D be added.

 

5. EF-based new framework simple three layers

UI-> BLL-> IDAL-> DAL-> MODEL

1. The presentation layer directly uses the business layer object

2. each specific operation class at the business layer inherits from the business parent class, defines a data layer interface IDAL in the business parent class, and defines an abstract method in the business parent class; the business subclass overrides the abstract method and instantiates the IDAL interface object in the parent class.

3. Each specific operation class on the data layer inherits from the data parent class and has a common addition, deletion, modification, and query method for the parent class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.