Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Entity Framework manipulates entities using LINQ

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how Entity Framework uses LINQ to operate entities. Xiaobian thinks it is quite practical, so share it with you as a reference. I hope you can gain something after reading this article.

What is LINQ TO Entities

LINQ, which stands for Language-INtegrated Query, is a technique for querying data in the. NET language. LINQ to Entities is a mechanism that facilitates querying conceptual models using LINQ.

Because LINQ is a declarative language, it allows us to focus on what data we need rather than how we should retrieve it. LINQ to Entities provides a good abstraction over the entity data model, so we can use LINQ to specify what data to retrieve, and then the LINQ to Entities provider handles accessing the database and fetching the necessary data for us.

LINQ to Entities Execution Process

When we use LINQ to Entities to execute LINQ queries against an entity data model, these LINQ queries are compiled first to determine what data we need to retrieve, then the compiled statements are executed, and ultimately return CLR objects that are understood by. NET from an application perspective.

上图展示了LINQ to Entities依赖EntityClient才能够使用EF的概念数据模型

执行流程:

1、应用程序创建一个LINQ查询。

2、LINQ to Entities会将该LINQ查询转换成EntityClient命令。

3、EntityClient命令然后使用EF和实体数据模型将这些命令转换成SQL查询。

4、然后会使用底层的ADO.NET provider将该SQL查询传入数据库。

5、该查询然后在数据库中执行。

6、执行结果返回给EF。

7、EF然后将返回的结果转换成CLR类型,比如领域实体。

8、EntityClient使用项目,并返回必要的结果给应用程序。

三、使用LINQ to Entities操作实体

使用LINQ查询的方式有两种:

1、查询语法。

2、方法语法。

选择哪种语法完全取决于你的习惯,两种语法的性能是一样的。查询语法相对更容易理解,但是灵活性稍差;相反,方法语法理解起来有点困难,但是提供了更强大的灵活性。使用方法语法可以进行链接多个查询,因此在单个语句中可以实现最大的结果。

下面以一个简单的例子来理解一下这两种方法的区别。创建一个控制台程序,并命名。

1、定义领域实体:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace LINQDemo.Model{ public class Book { public int Id { get; set; } public string Name { get; set; } public string Author { get; set; } public DateTime PublicationDate { get; set; } }}2、定义初始化器,并写入初始化数据using LINQDemo.Model;using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace LINQDemo.EF{ public class SeedingDataInitializer : DropCreateDatabaseAlways { /// /// 初始化种子数据 /// /// protected override void Seed(Context context) { context.Books.Add(new Book { Name = "C#高级编程(第十版)", Author = "小明", PublicationDate = Convert.ToDateTime("2017-12-11 12:12:45") }); context.Books.Add(new Book { Name = "oracle从入门到精通", Author = "张三", PublicationDate = Convert.ToDateTime("2015-12-11 12:12:45") }); context.Books.Add(new Book { Name = "JavaScript高级编程", Author = "李四", PublicationDate = Convert.ToDateTime("2011-09-11 12:12:45") }); base.Seed(context); } }}3、定义数据库上下文类using LINQDemo.Model;using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace LINQDemo.EF{ public class Context :DbContext { public Context() : base("AppConnection") { Database.SetInitializer(new SeedingDataInitializer()); } // 添加到数据上下文 public virtual DbSet Books { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // 设置生成的表名:Book modelBuilder.Entity().ToTable("Book"); base.OnModelCreating(modelBuilder); } }}4、Main函数定义如下:using LINQDemo.EF;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace LINQDemo{ class Program { static void Main(string[] args) { using (var context = new Context()) { #region 查询语法 var result = from p in context.Books where p.Name.Equals("JavaScript高级编程") select p; #endregion #region 方法语法 var books = context.Books.Where(p => p.Name.Equals("JavaScript高级编程")); foreach (var item in books) { Console.WriteLine(string.Format("书名:{0},作者:{1},发布时间:{2}", item.Name, item.Author, item.PublicationDate)); } #endregion } Console.WriteLine("完成"); Console.ReadKey(); } }}5、运行程序,查看结果

查看数据库结果:

Query syntax and method syntax are executed with the same result.

Key understanding:

When using LINQ to Entities, it is important to understand when to use IEnumerable and IQueryable. If IEnumerable is used, the query is executed immediately; if IQueryable is used, the query is not executed until the application requests enumeration of the query results, i.e., the query is delayed until the enumeration of the query results.

How do I decide whether to use IEnumerable or IQueryable? Using IQueryable gives you the opportunity to create a complex LINQ query that uses multiple statements without having to execute queries against the database for each query statement. This query is executed only if the final LINQ query requires enumeration.

About "Entity Framework how to use LINQ operation entity" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report