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

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

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


当前回答

我发现在使用EF时,我不能在同一个数据库模型中使用多个数据库。但在linq2sql中,我只需在模式名称前面加上数据库名称即可。

这是我最初开始使用linq2sql的原因之一。我不知道EF是否已经允许这一功能,但我记得读到它的初衷是不允许这一点。

其他回答

LINQ to SQL仅支持Microsoft SQL Server中可用的数据库表、视图、存储过程和函数的1对1映射。它是一个很好的API,可用于快速构建设计相对良好的SQL Server数据库的数据访问。LINQ2SQL首次与C#3.0和.Net Framework 3.5一起发布。

LINQ to Entities(ADO.Net Entity Framework)是一个ORM(对象关系映射器)API,它允许广泛定义对象域模型及其与许多不同ADO.Net数据提供程序的关系。因此,您可以混合和匹配许多不同的数据库供应商、应用程序服务器或协议,以设计由各种表、源、服务等构建的对象的聚合组合。ADO.Net Framework是与.Net Framework 3.5 SP1一起发布的。

这是一篇很好的MSDN入门文章:将LINQ引入关系数据

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

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.

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

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

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倍。。。

我认为,如果您需要快速开发一些东西,而中间没有奇怪的东西,那么您需要有代表您的表的实体:

Linq2Sql可以是一个很好的联盟,将它与LinQ一起使用可以释放出一个良好的开发时机。