在。net 4.0中,实体框架和LINQ到SQL的区别是什么?

时间:2022-02-04 19:03:02

I was checking 2nd edition of Professional ASP.NET MVC and 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.

我在看第二版的专业ASP。NET MVC和实现的EF将LINQ替换为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 question people 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也不是微软家族的SQL服务器。在这个2岁的问题中,人们对EF不满意,说它太复杂了。但现在我读的是EF的代码。只有类是用ADO生成的。从LINQ到SQL的实体模型。既然EF功能是实际上的标准技术ORM,谁能澄清人们对它的大惊小怪呢?

4 个解决方案

#1


73  

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.
  • LINQ to SQL主要是SQL Server,不是通过设计实现的。如果您有一个兼容的ADO, EF设计用于支持并支持多个DBs。净提供者。
  • 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.
  • 在开箱即用的情况下,LINQ to SQL对于DB元数据更改的描述非常糟糕。您必须从头重新生成模型的部分,并且您将丢失自定义。
  • The EF supports model features like many-to-many relationships and inheritance. LINQ to SQL does not directly support these.
  • EF支持模型特性,如多对多关系和继承。LINQ to SQL并不直接支持这些。
  • 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.
  • 在。net 3.5中,LINQ to SQL比EF更支持SQL- server特定的功能。这在。net 4中是不正确的;在这方面他们是相当相似的。
  • 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.
  • EF允许您首先选择模型,DB优先,或代码优先建模。LINQ to SQL,开箱即用,实际上只支持DB。

#2


17  

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-DB code use EF4.

EF随着v4.0时代的到来。在那之前,使用它有点痛苦,我不推荐它。现在我的建议是所有新的linqto - 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和SQL非常相似。但它是一个完全不同的体系结构:EF4充当一个(EF) ADO的LINQ提供程序。然后包装另一个ADO的网络提供者。净提供者。这里有一些新东西,比如实体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到SQL中实现这一点的唯一方法是通过“selector列”(尽管我从未在LINQ到SQL中尝试过)。

#3


13  

Difference between LINQ to SQL and Entity Framework:

LINQ to SQL:

LINQ to SQL:

  • It only works with SQL Server Database.
  • 它只适用于SQL Server数据库。
  • It generates a .dbml to maintain the relation
  • 它生成一个.dbml来维护关系
  • 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.
  • 它允许您使用DataContext查询数据。
  • It provides a tightly coupled approach.
  • 它提供了一种紧密耦合的方法。
  • It can be used for rapid application development only with SQL Server.
  • 它只能用于SQL Server的快速应用程序开发。

Entity Framework

实体框架

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

    它可以与Oracle、DB2、MYSQL、SQL Server等数据库协同工作。

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

    它最初生成一个.edmx文件。使用3个不同的文件来维护这个关系。

  • 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.

    它允许您使用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.

    它可以用于快速的应用程序开发,如SQL Server、Oracle、DB2和MySQL等。

More details

更多的细节

#4


4  

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体验(或者如果您尝试在没有设计器的情况下使用配置)。模型可以稍对象现在而不是一些设计师驱动部分类破坏bs的内在加上设计师驱动代码,你可以远离设计师完全没有感觉你游泳上游,一般来说它只是感觉他们听过社区或者试图模仿现有的解决方案进行战斗,而不是做一个版本的“庄”或L2Sql应该是什么。

#1


73  

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.
  • LINQ to SQL主要是SQL Server,不是通过设计实现的。如果您有一个兼容的ADO, EF设计用于支持并支持多个DBs。净提供者。
  • 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.
  • 在开箱即用的情况下,LINQ to SQL对于DB元数据更改的描述非常糟糕。您必须从头重新生成模型的部分,并且您将丢失自定义。
  • The EF supports model features like many-to-many relationships and inheritance. LINQ to SQL does not directly support these.
  • EF支持模型特性,如多对多关系和继承。LINQ to SQL并不直接支持这些。
  • 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.
  • 在。net 3.5中,LINQ to SQL比EF更支持SQL- server特定的功能。这在。net 4中是不正确的;在这方面他们是相当相似的。
  • 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.
  • EF允许您首先选择模型,DB优先,或代码优先建模。LINQ to SQL,开箱即用,实际上只支持DB。

#2


17  

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-DB code use EF4.

EF随着v4.0时代的到来。在那之前,使用它有点痛苦,我不推荐它。现在我的建议是所有新的linqto - 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和SQL非常相似。但它是一个完全不同的体系结构:EF4充当一个(EF) ADO的LINQ提供程序。然后包装另一个ADO的网络提供者。净提供者。这里有一些新东西,比如实体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到SQL中实现这一点的唯一方法是通过“selector列”(尽管我从未在LINQ到SQL中尝试过)。

#3


13  

Difference between LINQ to SQL and Entity Framework:

LINQ to SQL:

LINQ to SQL:

  • It only works with SQL Server Database.
  • 它只适用于SQL Server数据库。
  • It generates a .dbml to maintain the relation
  • 它生成一个.dbml来维护关系
  • 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.
  • 它允许您使用DataContext查询数据。
  • It provides a tightly coupled approach.
  • 它提供了一种紧密耦合的方法。
  • It can be used for rapid application development only with SQL Server.
  • 它只能用于SQL Server的快速应用程序开发。

Entity Framework

实体框架

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

    它可以与Oracle、DB2、MYSQL、SQL Server等数据库协同工作。

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

    它最初生成一个.edmx文件。使用3个不同的文件来维护这个关系。

  • 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.

    它允许您使用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.

    它可以用于快速的应用程序开发,如SQL Server、Oracle、DB2和MySQL等。

More details

更多的细节

#4


4  

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体验(或者如果您尝试在没有设计器的情况下使用配置)。模型可以稍对象现在而不是一些设计师驱动部分类破坏bs的内在加上设计师驱动代码,你可以远离设计师完全没有感觉你游泳上游,一般来说它只是感觉他们听过社区或者试图模仿现有的解决方案进行战斗,而不是做一个版本的“庄”或L2Sql应该是什么。