24

We have some legacy code that uses Linq to SQL as the ORM. We'd like to migrate this logic to .Net Core so that we can house it on a linux server. As far as I can tell, L2S is not included in .Net Core.

What is the migration path of least resistance?

3 Answers 3

11

If you used L2S because EF is inefficient at using Skip and Take for fetching large results as chunks, then your best bet is Dapper. Get yourself a copy of LINQPad and use it to get the generated SQL for each of your LINQ expressions.

L2S wraps some bizarre SQL around the actual query to use SQL's rownumber function to implement skip and take. If you are using the latest version of SQL Server then you don't need this because TSQL now has clauses equivalent to skip and take. This is handy if you are writing SQL directly and produces comprehensible SQL that won't induce WTF in those who follow, but the LINQ way works on all versions of SQL Server.

Then use this SQL with Dapper, which will do the ORM part for you. It also has proper support for type mapping parameters similar to L2S so you can avoid building SQL strings and injection vulnerabilities.

If you want all the smarts for constructing object graphs with FK values implied by collection membership then you're out of luck, you'll have to code it by hand.

update 2018-05-11

EF is less horrible than it used to be. EF Core is simpler than EF while retaining many of the benefits. I am currently using EF Core on a project at work and it's not the disaster EF once was.

I did have to help with an outer join. Left to its own devices, LINQ fetched the inner part and then for each inner row ran a separate query for its outer portion.

I fixed this by explicitly fetching the inner part and constructing a keyset as an array of int. Another LINQ statement fetched all of the outer rows exploiting the fact that Array.Contains maps to IN which uses indexes. Then I materialised both parts using ToArray() and used LINQ to join them in memory. This brought execution time down from ten minutes to 300ms.

You shouldn't have to do this; L2S wouldn't have cocked it up in the first place. But at least there's a straightforward general solution.

A shortcoming of my solution is that it is not well adapted to progressive fetch.

update 2020-06-12

Dapper can return dynamic results. I find this is a good bridge between SQL and C#. The columns and their names are governed by the SQL. This can be a little fragile since C# is case sensitive and SQL isn't, but with the SQL in your code you can at least see what it is and fix it.

More to the point, you can use LINQ directly on these dynamic results.

7
  • 7
    Ha I used linq2sql because EF didn’t exist! Jul 4, 2018 at 21:00
  • 5
    @Simon_Weaver I remember those happy days. And we have come full circle: the more screwed up a database is, the more useless EF is. And any corporate database that's actually used is always a pig's breakfast. Dapper becomes the only practical option.
    – Peter Wone
    Jul 5, 2018 at 1:48
  • 1
    It's 2 years later, and we never did refactor this. However, we have used Dapper for some new projects going forward, and it's pretty good. Jul 5, 2018 at 14:05
  • 2
    @PeterWone L2S was not improved for more then a decade and still, it is faster and has features that EF Classic and latest EF Core do not have combined (v3.1). Namely, support for insert/update stored procedures and batch updates at the same time. EF Classic has stored procedures, but no batching. EF Core has batching, but no write stored procedures. Additionally, it was always significantly more performant than EF Classic. Regarding outer join, L2S DataContext has LoadOptions where you configure LoadWith<InnerTable>(inner => inner.OutetTable) to fetch all data at once.
    – Nenad
    Jun 11, 2020 at 9:22
  • @Nenad you don't need to sell me on the virtues of L2S, I already know. But it's not available in the brave new dotnet core world so that's that. The OneCore project converges the two and the SQL team actively wants to kill L2S so you can expect L2S to stop working at some point.
    – Peter Wone
    Jun 12, 2020 at 0:53
3

DevArt's LinqConnect now support .NET Core as of May 2017:

https://www.devart.com/news/2017/net-core-support.html

It's a drop in replacement for Linq To Sql and it even dislocates you from MS SQL Server if you so wish!

4
  • devart.com/linqconnect/editions.html Standard is $150, Pro is $250, Express is free but doesn't support .NET Core and has a 10 entity limit.
    – Emyr
    Nov 22, 2018 at 12:12
  • What is that? Per developer licenses? @Emyr
    – RoboJ1M
    Nov 27, 2018 at 9:48
  • 1
    Those are the single license prices, team (one company, 4 devs, one physical address) and site (one company, infinite devs, one physical address) prices are listed at devart.com/linqconnect/ordering.html It'd be easier for my team to spend time migrating to a more common ORM that has Core support (EF, Dapper, etc) than to work this stuff out and then work out how to get this through my org's sourcing and finance teams, especially since it's likely any colleagues I work with could be in another city or country.
    – Emyr
    Dec 7, 2018 at 12:32
  • @Emyr Can you easily port from LinqToSql to EF? Here we have been raised on the ago old method of formatted SQL statements in string constants This maps really well to LinqToSql where you have LinqQueries make expected SQL Can Linq To Entities work like this? Our primary goal moving forward is to have the database schema locked down both on customer site and here in our office We no longer want support start and config staff making local changes to the database scheme, especially as the changes are rarely documented and never sent back down to development so our schema are up to date.
    – RoboJ1M
    Dec 18, 2018 at 10:38
2

If you are rewriting legacy code to .NET Core, this will take some effort to being with.

And for L2S, you will probably need to rewrite this into modern queries using Entity Framework Core. It might make your life easier generating entities from database though, see Reverse engineer your model.

This would be the recommended way, however I am not sure if it's the easiest one in your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.