Entity Framework vs LINQ to SQL

后端 未结 17 1942
离开以前
离开以前 2020-11-22 09:25

Now that .NET v3.5 SP1 has been released (along with VS2008 SP1), we now have access to the .NET entity framework.

My question is this. When trying to decide betwee

相关标签:
17条回答
  • 2020-11-22 09:55

    LINQ to SQL only supports 1 to 1 mapping of database tables, views, sprocs and functions available in Microsoft SQL Server. It's a great API to use for quick data access construction to relatively well designed SQL Server databases. LINQ2SQL was first released with C# 3.0 and .Net Framework 3.5.

    LINQ to Entities (ADO.Net Entity Framework) is an ORM (Object Relational Mapper) API which allows for a broad definition of object domain models and their relationships to many different ADO.Net data providers. As such, you can mix and match a number of different database vendors, application servers or protocols to design an aggregated mash-up of objects which are constructed from a variety of tables, sources, services, etc. ADO.Net Framework was released with the .Net Framework 3.5 SP1.

    This is a good introductory article on MSDN: Introducing LINQ to Relational Data

    0 讨论(0)
  • 2020-11-22 09:56

    Is LINQ to SQL Truly Dead? by Jonathan Allen for InfoQ.com

    Matt Warren describes [LINQ to SQL] as something that "was never even supposed to exist." Essentially, it was just supposed to be stand-in to help them develop LINQ until the real ORM was ready.

    ...

    The scale of Entity Framework caused it to miss the .NET 3.5/Visual Studio 2008 deadline. It was completed in time for the unfortunately named ".NET 3.5 Service Pack 1", which was more like a major release than a service pack.

    ...

    Developers do not like [ADO.NET Entity Framework] because of the complexity.

    ...

    as of .NET 4.0, LINQ to Entities will be the recommended data access solution for LINQ to relational scenarios.

    0 讨论(0)
  • 2020-11-22 09:57

    My experience with Entity Framework has been less than stellar. First, you have to inherit from the EF base classes, so say good bye to POCOs. Your design will have to be around the EF. With LinqtoSQL I could use my existing business objects. Additionally, there is no lazy loading, you have to implement that yourself. There are some work arounds out there to use POCOs and lazy loading, but they exist IMHO because EF isn't ready yet. I plan to come back to it after 4.0

    0 讨论(0)
  • 2020-11-22 10:00

    Here's some metrics guys... (QUANTIFYING THINGS!!!!)

    I took this query where I was using Entity Framework

    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();
    

    and changed it into this where I'm using the repository pattern 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-to-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();
    

    Also, please know that Linq-to-Sql is 14x faster than Linq...

    0 讨论(0)
  • 2020-11-22 10:01

    I found that I couldn't use multiple databases within the same database model when using EF. But in linq2sql I could just by prefixing the schema names with database names.

    This was one of the reasons I originally began working with linq2sql. I do not know if EF has yet allowed this functionality, but I remember reading that it was intended for it not to allow this.

    0 讨论(0)
  • 2020-11-22 10:03

    There are a number of obvious differences outlined in that article @lars posted, but short answer is:

    • L2S is tightly coupled - object property to specific field of database or more correctly object mapping to a specific database schema
    • L2S will only work with SQL Server (as far as I know)
    • EF allows mapping a single class to multiple tables
    • EF will handle M-M relationships
    • EF will have ability to target any ADO.NET data provider

    The original premise was L2S is for Rapid Development, and EF for more "enterprisey" n-tier applications, but that is selling L2S a little short.

    0 讨论(0)
提交回复
热议问题