Monday, January 15, 2018
Linq To Sql Introduction and Creating NorthWind Database
Linq To Sql Introduction and Creating NorthWind Database
LINQ to SQL provides a runtime infrastructure for managing relational data as objects without losing the ability to query. Your application is free to manipulate the objects while LINQ to SQL stays in the background tracking your changes automatically.
Advantages L2S offers:
No magic strings, like you have in SQL queries
Intellisense
Compile check when database changes
Faster development
Unit of work pattern (context)
Auto-generated domain objects that are usable small projects
Lazy loading.
Learning to write linq queries/lambdas is a must learn for .NET developers.
Regarding performance:
Most likely the performance is not going to be a problem in most solutions. To pre-optimize is an anti-pattern. If you later see that some areas of the application are to slow, you can analyze these parts, and in some cases even swap some linq queries with stored procedures or ADO.NET.
In many cases the lazy loading feature can speed up performance, or at least simplify the code a lot.
Regarding debuging:
In my opinion debuging Linq2Sql is much easier than both stored procedures and ADO.NET. I recommend that you take a look at Linq2Sql Debug Visualizer, which enables you to see the query, and even trigger an execute to see the result when debugging.
You can also configure the context to write all sql queries to the console window, more information here
Regarding another layer:
Linq2Sql can be seen as another layer, but it is a purely data access layer. Stored procedures is also another layer of code, and I have seen many cases where part of the business logic has been implemented into stored procedures. This is much worse in my opinion because you are then splitting the business layer into two places, and it will be harder for developers to get a clear view of the business domain.
For Linq to Sql Session we will be working on Sql s Northwind Database.
Creating The Northwind Database:
You can Download The database from here: NorthWind
The Complete backup of Northwind database: Northwind
Now Next thing is to Create a New LINQ to SQL Data Model.
For all the Next Examples you will need This Class.
1. Create a New Project Name is LinqToSqlExample.
2. Now Go To Project -> Add New Item and Add a Linq To Sql File and Name it NorthWind.
3. You will see a screen like below.
Now we will add a Data Connection as shown in above figure in left pane.
Select Add Connection.. And you will see below screen.
Press ok and you will see below screen.
And now our class is created . we will be using this in our next examples.
Advantages L2S offers:
No magic strings, like you have in SQL queries
Intellisense
Compile check when database changes
Faster development
Unit of work pattern (context)
Auto-generated domain objects that are usable small projects
Lazy loading.
Learning to write linq queries/lambdas is a must learn for .NET developers.
Regarding performance:
Most likely the performance is not going to be a problem in most solutions. To pre-optimize is an anti-pattern. If you later see that some areas of the application are to slow, you can analyze these parts, and in some cases even swap some linq queries with stored procedures or ADO.NET.
In many cases the lazy loading feature can speed up performance, or at least simplify the code a lot.
Regarding debuging:
In my opinion debuging Linq2Sql is much easier than both stored procedures and ADO.NET. I recommend that you take a look at Linq2Sql Debug Visualizer, which enables you to see the query, and even trigger an execute to see the result when debugging.
You can also configure the context to write all sql queries to the console window, more information here
Regarding another layer:
Linq2Sql can be seen as another layer, but it is a purely data access layer. Stored procedures is also another layer of code, and I have seen many cases where part of the business logic has been implemented into stored procedures. This is much worse in my opinion because you are then splitting the business layer into two places, and it will be harder for developers to get a clear view of the business domain.
For Linq to Sql Session we will be working on Sql s Northwind Database.
Creating The Northwind Database:
You can Download The database from here: NorthWind
The Complete backup of Northwind database: Northwind
Now Next thing is to Create a New LINQ to SQL Data Model.
For all the Next Examples you will need This Class.
1. Create a New Project Name is LinqToSqlExample.
2. Now Go To Project -> Add New Item and Add a Linq To Sql File and Name it NorthWind.
3. You will see a screen like below.
Now we will add a Data Connection as shown in above figure in left pane.
Select Add Connection.. And you will see below screen.
Press ok and you will see below screen.
And now our class is created . we will be using this in our next examples.