简体   繁体   English

Linq对SQL有什么问题?

[英]What's wrong with Linq to SQL?

What's wrong with Linq to SQL? Linq对SQL有什么问题?

Or - what about Linq to SQL would make it unsuitable for a project, either new or existing? 或者 - Linq to SQL会使它不适合新项目或现有项目吗? I want to hear about why you would not choose Linq to SQL for a particular project - including what project parameters make it unsuitable. 我想知道为什么你为某个特定项目选择Linq to SQL - 包括哪些项目参数使它不合适。

It is not very adaptable to changes in the database schema. 它不太适合数据库模式的更改。 You have to rebuild the dbml layer and regenerate your data contexts. 您必须重建dbml层并重新生成数据上下文。

Like any ORM (I am not getting into the debate as to whether it is an ORM or not), you do have to be aware what SQL is being generated, and how that will influence your calls. 像任何ORM一样(我不会讨论它是否是ORM),你必须要知道正在生成什么SQL,以及它将如何影响你的调用。

Inserts are not batched, so can be high cost in performance. 插件不是批量生产的,因此性能成本很高。

It's being sunsetted in favour of Entity Framework 它正在落后于实体框架

Despite the fact it is using a provider model that will allow providers to be built for other DBMS platforms, only SQL Server is supported. 尽管它使用的提供程序模型允许为其他DBMS平台构建提供程序,但只支持SQL Server。

[ EDIT @ AugustLights - In my experience: ] Lazy loading may take a bit of hacking to get working. [ 编辑 @AugustLights - 根据我的经验:]延迟加载可能需要一些黑客才能开始工作。

That being said, I think it it is very handy if used correctly 话虽如此,我认为如果使用得当它非常方便

For a project that needs to make use of databases other than SQL Server: 对于需要使用SQL Server以外的数据库的项目:

1) You are locked in to using SQL Server 1)您被锁定使用SQL Server

For a project with complex entity relations and/or relations that change gradually over time: 对于具有复杂实体关系和/或随时间逐渐变化的关系的项目:

2) You are locked in to 1-to-1 mapping of tables to classes 2)您被锁定为表到类的一对一映射

For a project that must use 1.x versions of .NET 对于必须使用1.x版.NET的项目

3) Won't work with .NET 1.x 3)不适用于.NET 1.x.

  • There is no way to mix-n-match lazy loading / eager loading on a datacontext. 没有办法在datacontext上混合使用延迟加载/预加载。
  • True persistance ignorance is very difficult. 真正的坚持无知是非常困难的。
  • Mapping options are limited. 映射选项有限。 For example, there are no many-to-many relationships. 例如,没有多对多的关系。
  • Resyncing the mapping with schema changes is painful. 使用模式更改重新同步映射非常痛苦。

Despite all of the above I think linq-to-sql is an excellent choice for many projects. 尽管如此,我认为linq-to-sql是许多项目的绝佳选择。

It is difficult to mock while unit testing because of a lack of an interface on the System.Data.Linq.DataContext class. 由于System.Data.Linq.DataContext类缺少接口,因此在单元测试时很难进行模拟。 Here's a possible approach: Mocking LINQ to SQL DataContext . 这是一种可能的方法:将LINQ模拟为SQL DataContext

因为你没有使用3.5 ...这是一个有效的答案吗?!?!?

Well, I have developed some applications using LINQ to SQL. 好吧,我已经使用LINQ to SQL开发了一些应用程序。 One of the main problems that I find is having to layer your application. 我发现的一个主要问题是必须分层你的应用程序。 In LINQ to SQL the entity classes are tied very closely with the data access code. 在LINQ to SQL中,实体类与数据访问代码紧密相关。 Also, there are some issues with DataContext which means that you can use a DataContext object to retrieve an item but you cannot transfer the item (object) to another DataContext (at least not easily). 此外,DataContext存在一些问题,这意味着您可以使用DataContext对象来检索项目,但不能将项目(对象)传输到另一个DataContext(至少不容易)。

LINQ to SQL will be useful if you don't care about layering your application properly and also if all you wanted is to create an application in a rapid manner also know as RAPID Application Development. 如果您不关心正确分层应用程序,并且如果您想要的是快速创建应用程序(也称为RAPID应用程序开发),LINQ to SQL将非常有用。

A lot of the advantage to LINQ-to-SQL comes from supposedly being able to construct data queries right in your code-behind based on strongly-typed queryable/enumerable data objects from your dbml (which plays the role of a very limited DAL). LINQ-to-SQL的许多优点来自于基于dbml中强类型可查询/可枚举数据对象(它扮演非常有限的DAL的角色),可以在代码隐藏中构建数据查询。 。 So a consequence, as has already been mentioned, is that it encourages you somewhat towards playing outside strongly defined and separated layers or tiers to your application. 因此,正如已经提到的那样,结果是它鼓励您在某种程度上在您的应用程序之外的强定义和分离的层或层之外进行游戏。
To counter that point, it should mean that you should be able to eliminate most or all of any business logic you were writing into stored procedures on the database, so then at least you only have to go to the code that deals with the data to change non-schema-impacting business rules... However, that breaks down a bit when you realise how complicated it can be to write a query with an outer join with aggregates with grouping, at least when you first approach it. 为了反驳这一点,它应该意味着你应该能够消除你在数据库中写入存储过程的大部分或全部业务逻辑,所以至少你只需要去处理数据的代码更改非模式影响业务规则...但是,当您意识到使用带有分组的聚合的外部联接编写查询时会有多么复杂,至少在您第一次接近时是这样。 So you'll be tempted to write the sprocs in the SQL you know that is so simple and good at doing those things rather than spend the extra time trying to figure out the LINQ syntax to do the same thing when it's just going to convert it to ugly SQL code anyway... 所以你很想在SQL中编写sprocs,你知道这样做很简单,很擅长做这些事情,而不是花费额外的时间来试图找出LINQ语法来做同样的事情,当它只是转换它时无论如何要丑陋的SQL代码......
That having been said, I really do love LINQ, and my esteem for it vastly increased when I started ignoring this "query syntax is easier to read" sentiment I've seen floating around and switched to method syntax. 话虽如此,我真的很喜欢LINQ,当我开始忽略这种“查询语法更容易阅读”的情绪时,我对它的尊重大大增加了我看到的情绪,我已经看到了并且切换到方法语法。 Never looked back. 永不回头。

The only thing I would label as a technical "showstopper" is if you want to use other RDBMSes than SQL Server. 我唯一要标记为技术“showstopper”的是你想要使用其他RDBMS而不是SQL Server。 (although it can be worked around - see Matt Warren's blog @ http://blogs.msdn.com/mattwar/ ) (虽然它可以解决 - 见Matt Warren的博客@ http://blogs.msdn.com/mattwar/

Besides that, there are some pros and cons already listed in previous answers to your question. 除此之外,您的问题的先前答案中已经列出了一些优点和缺点。 However, all of the negatives mentioned so far have workarounds so they are not really showstoppers. 但是,到目前为止提到的所有负面结果都有变通方法,所以它们并不是真正的显示器。

A non-technical [potential] showstopper is the risk that MSFT will abandon it in favour of EF... More on that here: http://oakleafblog.blogspot.com/2008/05/is-adonet-team-abandoning-linq-to-sql.html 非技术性[潜在] showstopper是MSFT放弃它而支持EF的风险......更多关于这一点: http//oakleafblog.blogspot.com/2008/05/is-adonet-team-abandoning- LINQ到sql.html

Although (in my opinion, ) the current state of EF is reason enough for them to continue work on L2S. 虽然(在我看来),EF的当前状态足以让他们继续在L2S上工作。 So let's hope they do... 所以我们希望他们这样做......

A true ORM should separate the design of your Business Entities from your persistence medium. 真正的ORM应该将业务实体的设计与持久性介质分开。 That way you can refactor either one of them separately and only have to maintain the mapping between the two. 这样你就可以分别重构其中任何一个,只需要维护两者之间的映射。 This reduces the amount of application logic code that you need to maintain for database changes. 这减少了为数据库更改而需要维护的应用程序逻辑代码量。

To accomplish this kind of persistence agnostic approach with Linq-to-SQL, you would have to use its generated classes at DTOs and maintain a mapping logic between your DTOs and your Entities. 要使用Linq-to-SQL实现这种持久性不可知的方法,您必须在DTO中使用其生成的类,并在DTO和您的实体之间维护映射逻辑。

There are much better ORMs for taking this approach (like NHibernate) that greatly reduce the need for DTOs. 采用这种方法(如NHibernate)有更好的ORM,可以大大减少对DTO的需求。

似乎对DB列的默认值没有任何支持。

This question was asked once before over here . 这个问题被问过一次在这里 But, in essence, LINQ to SQL generates sub-optimal execution plans in your database. 但是,从本质上讲,LINQ to SQL会在您的数据库中生成次优执行计划。 For every different length of parameter you search for, it will force the creation of a different execution plan. 对于您搜索的每个不同长度的参数,它将强制创建不同的执行计划。 This will eventually clog up the memory in your database that is being used to cache execution plans and you will start expiring older queries, which will need to be recompiled when they come up again. 这最终会阻塞数据库中用于缓存执行计划的内存,并且您将开始使旧查询过期,这些查询在再次出现时需要重新编译。

As I mentioned in the question I linked to, it's a matter of what you're trying to accomplish. 正如我在与之相关的问题中所提到的,这是你想要完成的事情。 If you're willing to trade execution speed for development speed, LINQ to SQL might be a good choice. 如果您愿意以执行速度交换开发速度,LINQ to SQL可能是一个不错的选择。 If you're concerned about execution speed, there are other ORMs/DALs/solutions available that may take longer to work with but will provide you with future proofing against schema changes and better performing solutions at the cost of additional development overhead. 如果您担心执行速度,可以使用其他可能需要更长时间的ORM / DAL /解决方案,但会为您提供针对架构更改和更好性能解决方案的未来验证,但需要额外的开发开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM