现在,.NET v3.5 SP1已经发布(与VS2008 SP1一起发布),我们现在可以访问.NET实体框架。

我的问题是这个。当试图决定使用实体框架和LINQ to SQL作为ORM时,有什么区别?

按照我的理解,实体框架(当与LINQ to Entities一起使用时)是LINQ to SQL的“大哥”?如果是这样的话,它有什么好处?LINQ to SQL不能单独做什么?


当前回答

这里有一些指标。。。(量化事物!!!)

我在使用实体框架的地方进行了这个查询

var result = (from metattachType in _dbContext.METATTACH_TYPE
                join lineItemMetattachType in _dbContext.LINE_ITEM_METATTACH_TYPE on metattachType.ID equals lineItemMetattachType.METATTACH_TYPE_ID
                where (lineItemMetattachType.LINE_ITEM_ID == lineItemId && lineItemMetattachType.IS_DELETED == false
                && metattachType.IS_DELETED == false)
                select new MetattachTypeDto()
                {
                    Id = metattachType.ID,
                    Name = metattachType.NAME
                }).ToList();

并将其更改为我使用的存储库模式Linq公司

            return await _attachmentTypeRepository.GetAll().Where(x => !x.IsDeleted)
                .Join(_lineItemAttachmentTypeRepository.GetAll().Where(x => x.LineItemId == lineItemId && !x.IsDeleted),
                attachmentType => attachmentType.Id,
                lineItemAttachmentType => lineItemAttachmentType.MetattachTypeId,
                (attachmentType, lineItemAttachmentType) => new AttachmentTypeDto
                {
                    Id = attachmentType.Id,
                    Name = attachmentType.Name
                }).ToListAsync().ConfigureAwait(false);

Linq到sql

            return (from attachmentType in _attachmentTypeRepository.GetAll()
                    join lineItemAttachmentType in _lineItemAttachmentTypeRepository.GetAll() on attachmentType.Id equals lineItemAttachmentType.MetattachTypeId
                    where (lineItemAttachmentType.LineItemId == lineItemId && !lineItemAttachmentType.IsDeleted && !attachmentType.IsDeleted)
                    select new AttachmentTypeDto()
                    {
                        Id = attachmentType.Id,
                        Name = attachmentType.Name
                    }).ToList();

另外,请知道Linq to Sql比Linq快14倍。。。

其他回答

在@lars发布的文章中,有许多明显的区别,但简短的回答是:

L2S是紧密耦合的-对象属性到数据库的特定字段,或者更正确地将对象映射到特定数据库模式L2S仅适用于SQL Server(据我所知)EF允许将单个类映射到多个表EF将处理M-M关系EF将能够针对任何ADO.NET数据提供程序

最初的前提是L2S用于快速开发,EF用于更多的“企业级”n层应用程序,但这使L2S的销售有点不足。

如果您的数据库简单明了,LINQ to SQL就可以了。如果您需要在表上添加逻辑/抽象实体,请使用实体框架。

我的印象是,如果Linq2Sql不符合您的需求,那么您的数据库就相当糟糕或者设计得非常糟糕。我有大约10个大大小小的网站都在使用Linq2Sql。我已经多次查看和实体框架,但我找不到在Linq2Sql上使用它的好理由。也就是说,我尝试使用我的数据库作为模型,所以我已经在模型和数据库之间建立了1对1的映射。

在我目前的工作中,我们有一个拥有200多个表的数据库。一个有很多糟糕解决方案的旧数据库,因此我可以看到实体框架优于Linq2Sql的好处,但我还是希望重新设计数据库,因为数据库是应用程序的引擎,如果数据库设计糟糕且速度慢,那么我的应用程序也会很慢。在这样的数据库上使用Entity框架似乎是一种快速修复方法,可以掩盖坏模型,但它永远无法掩盖从这样的数据库中获得的糟糕性能。

LINQ to SQL Entity Framework
It only works with SQL Server database It can work with various databases like Oracle, DB2, MySQL, SQL Server, etc.
It generates .dbml to maintain the relation It generates a .edmx file initially. The relation is maintained using 3 different files; .csdl, .msl and .ssdl
It has not to support for complex types It has support for complex types
It cannot generate database from model It can generate database from model
It allows only one-to-one mapping between the entity classes and the relational tables/views It allows one-to-one, one-to-many, many-to-many mappings between the entity classes and relational tables/views
It allows you to query data using DataContext It allows you to query data using EntitySQL, ObjectContext, DbContext
It can be used for rapid application development only with SQL Server It can be used for rapid application development with RDBMS like SQL Server, Oracle, PostgreSQL, etc.

两者都不支持唯一的SQL 2008数据类型。与我的观点不同的是,Entity仍然有机会在未来的某个版本中围绕我的地理数据类型构建模型,而Linq to SQL被放弃,永远不会。

不知道nHibernate或OpenAccess有什么问题。。。