实体框架和 .NET 4.0 的 LINQ to SQL 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3293995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:32:25  来源:igfitidea点击:

What is the difference between Entity Framework and LINQ to SQL by .NET 4.0?

.netlinq-to-sqlentity-framework.net-4.0

提问by Ufuk Hac?o?ullar?

I was checking 2nd edition of Professional ASP.NET MVCand realized EF replaced LINQ to SQL. I am familiar to LINQ to SQL from the first book but I know nothing about EF. Anyway while reading the code, it seems like nothing has changed except the name. Same old repository classes, same old functions.

我正在检查Professional ASP.NET MVC 的第二版,并意识到 EF 取代了 LINQ to SQL。我从第一本书就熟悉 LINQ to SQL,但我对 EF 一无所知。无论如何,在阅读代码时,除了名称之外似乎没有任何变化。相同的旧存储库类,相同的旧功能。

I did a little research. I know LINQ is not limited to SQL. Also EF is not limited Microsoft-family SQL servers. In this 2 year old questionpeople are not happy with EF, saying it's overcomplicated and all. But now I'm reading same code under EF name. Only classes are generated with ADO.NET Entity Model insted of LINQ to SQL. Can anybody clear out the fuss about EF features since it's the de facto standart ORM now?

我做了一点研究。我知道 LINQ 不仅限于 SQL。此外,EF 不受 Microsoft 家族 SQL 服务器的限制。在这个 2 年前的问题中,人们对 EF 不满意,说它过于复杂。但现在我正在阅读 EF 名称下的相同代码。仅使用 LINQ to SQL 插入的 ADO.NET 实体模型生成类。任何人都可以清除关于 EF 功能的大惊小怪,因为它现在是事实上的标准 ORM?

回答by Craig Stuntz

They are somewhat similar, and can be used in a very similar way, code-wise, but they have some important differences. Note that "LINQ" is not the same thing as "LINQ to SQL"; the EF also uses LINQ. Some notable differences are:

它们有些相似,并且可以以非常相似的方式使用,在代码方面,但它们有一些重要的区别。请注意,“LINQ”与“LINQ to SQL”不是一回事;EF 也使用 LINQ。一些显着的差异是:

  • LINQ to SQL is largely SQL Server only, not so much by design as by implementation. The EF is designed to support, and does support, multiple DBs, if you have a compatible ADO.NET provider.
  • Out of the box, LINQ to SQL has a very poor story for DB metadata changes. You have to regenerate parts of your model from scratch, and you lose customizations.
  • The EF supports model features like many-to-many relationships and inheritance. LINQ to SQL does not directly support these.
  • In .NET 3.5, LINQ to SQL had much better support for SQL-Server-specific functionality than the EF. This is mostly not true in .NET 4; they're fairly similar in that respect.
  • The EF lets you choose Model First, DB First, or Code First modeling. LINQ to SQL, out of the box, really only supports DB First.
  • LINQ to SQL 主要是 SQL Server,与其说是设计,不如说是实现。如果您有兼容的 ADO.NET 提供程序,EF 旨在支持并且确实支持多个 DB。
  • 开箱即用,LINQ to SQL 对于 DB 元数据更改有一个非常糟糕的故事。您必须从头开始重新生成模型的各个部分,并且会丢失自定义设置。
  • EF 支持多对多关系和继承等模型功能。LINQ to SQL 不直接支持这些。
  • 在 .NET 3.5 中,LINQ to SQL 对 SQL Server 特定功能的支持比 EF 好得多。这在 .NET 4 中大都不是真的;他们在这方面相当相似。
  • EF 允许您选择 Model First、DB First 或 Code First 建模。LINQ to SQL 开箱即用,实际上仅支持 DB First。

回答by Stephen Cleary

EF came of age with v4.0. Before that, it was a bit of a pain to use, and I didn't recommend it. Now my recommendation is that all new LINQ-to-DBcode use EF4.

EF 随着 v4.0 成熟。在此之前,使用起来有点痛苦,我并不推荐它。现在我的建议是所有新的 LINQ-to- DB代码都使用 EF4。

As far as new features go, the LINQ part is actually quite similar to LINQ to SQL. But it's quite a different architecture: EF4 acts as a LINQ provider to an (EF) ADO.NET provider which then wraps another ADO.NET provider. So there's new things like Entity SQL (which I don't use), and EF supporting different underlying ADO.NET providers (which I do use).

就新功能而言,LINQ 部分实际上与 LINQ to SQL 非常相似。但它是完全不同的体系结构:EF4 充当 (EF) ADO.NET 提供程序的 LINQ 提供程序,然后包装另一个 ADO.NET 提供程序。所以有一些新东西,比如 Entity SQL(我不使用)和 EF 支持不同的底层 ADO.NET 提供程序(我确实使用过)。

The XML modeling system that EF uses allows more powerful mapping abstractions, as well. One that I use regularly is having different tables with the same primary keys mapping to an entity inheritance relationship; from what I understand, the only way to do this in LINQ to SQL is via a "selector column" (though I never tried this in LINQ to SQL).

EF 使用的 XML 建模系统也允许更强大的映射抽象。我经常使用的一种是将具有相同主键的不同表映射到实体继承关系;据我所知,在 LINQ to SQL 中执行此操作的唯一方法是通过“选择器列”(尽管我从未在 LINQ to SQL 中尝试过)。

回答by IndieTech Solutions

Difference between LINQ to SQL and Entity Framework:

LINQ to SQL 和实体框架的区别:

LINQ to SQL:

LINQ 到 SQL:

  • It only works with SQL Server Database.
  • It generates a .dbml to maintain the relation
  • It has not support for complex type.
  • It cannot generate database from model.
  • It allows only one to one mapping between the entity classes and the relational tables /views.
  • It allows you to query data using DataContext.
  • It provides a tightly coupled approach.
  • It can be used for rapid application development only with SQL Server.
  • 它仅适用于 SQL Server 数据库。
  • 它生成一个 .dbml 来维护关系
  • 它不支持复杂类型。
  • 它无法从模型生成数据库。
  • 它只允许实体类和关系表/视图之间的一对一映射。
  • 它允许您使用 DataContext 查询数据。
  • 它提供了一种紧密耦合的方法。
  • 它只能用于 SQL Server 的快速应用程序开发。

Entity Framework

实体框架

  • It can works with various databases like Oracle, DB2, MYSQL, SQL Server etc.

  • It generates an .edmx files initially. The relation is maintained using 3 different files .csdl, .msl and .ssdl

  • It has support for complex type.

  • It can generate database from model.

  • It allows one-to-one, one-to-many & many-to-many mappings between the Entity classes and the relational tables /views

  • It allows you to query data using EntitySQL, ObjectContext, DbContext.

  • It provides a loosely coupled approach. Since its code first approach allow you to use Dependency Injection pattern which make it loosely coupled .

  • It can be used for rapid application development with RDBMS like SQL Server, Oracle, DB2 and MySQL etc.

  • 它可以与各种数据库一起使用,如 Oracle、DB2、MYSQL、SQL Server 等。

  • 它最初生成一个 .edmx 文件。使用 3 个不同的文件 .csdl、.msl 和 .ssdl 维护关系

  • 它支持复杂类型。

  • 它可以从模型生成数据库。

  • 它允许实体类和关系表/视图之间的一对一、一对多和多对多映射

  • 它允许您使用 EntitySQL、ObjectContext、DbContext 查询数据。

  • 它提供了一种松散耦合的方法。由于它的代码优先方法允许您使用依赖注入模式,这使其松散耦合。

  • 它可用于使用 RDBMS 进行快速应用程序开发,如 SQL Server、Oracle、DB2 和 MySQL 等。

More details

更多细节

回答by heisenberg

Latest EF is lot more robust, and you're not forced into a designer driven pseudo-ORM experience (or a morass of config if you tried to do it without the designer). Your model can be POCO objects now instead of some designer driven partial class mangled bs that's intrinsicly coupled with designer driven code, you can get away from the designer entirely without feeling like you're swimming upstream, and in general it just feels like they have listened to the community or actually tried to emulate existing, battle-tested solutions instead of making a version for the "Morts" or whatever L2Sql was supposed to be.

最新的 EF 更加健壮,并且您不会被迫进入设计器驱动的伪 ORM 体验(如果您尝试在没有设计器的情况下进行配置,则不会被迫进入配置的泥潭)。你的模型现在可以是 POCO 对象,而不是一些设计器驱动的部分类损坏 bs,它本质上与设计器驱动的代码耦合,你可以完全脱离设计器,而不会觉得你在逆流而上,总的来说,感觉就像他们有听取社区意见或实际尝试模仿现有的、经过实战考验的解决方案,而不是为“Morts”或任何 L2Sql 制作一个版本。

回答by M Faiz

LINQ to SQL gives permission you to query and modify SQL Server database by using LINQ syntax. Entity framework is a great ORM shipped by Microsoft which gives permission you to query and modify RDBMS like SQL Server, Oracle, DB2 and MySQL etc. by using LINQ syntax. Today, EF is widely used by each and every .NET application to query to database.

LINQ to SQL 允许您使用 LINQ 语法查询和修改 SQL Server 数据库。实体框架是微软提供的一个很好的 ORM,它允许你使用 LINQ 语法查询和修改 RDBMS,如 SQL Server、Oracle、DB2 和 MySQL 等。今天,EF 被每个.NET 应用程序广泛用于查询数据库。

In other words LINQ is used to connect C# code to in-memory objects of many different kinds. Entity Framework is an object-relational mapping (ORM) framework for connecting C# code to external databases, usually SQL Server. LINQ is a query language embedded into C# and a set of extension methods in order to make it useful.

换句话说,LINQ 用于将 C# 代码连接到许多不同类型的内存中对象。实体框架是一个对象关系映射 (ORM) 框架,用于将 C# 代码连接到外部数据库,通常是 SQL Server。LINQ 是一种嵌入到 C# 中的查询语言和一组扩展方法,以使其有用。