So sánh linq to sql và ado net entity framework năm 2024

Khi nào dùng Stored Procedure, SQL query? Khi chúng ta viết ứng dụng Client-Server, khá thuần về công nghệ, ít có sự tương tác với nhiều nguồn dữ liệu khác nhau. Ứng dụng logic có khả năng bảo trì được trong giới hạn cho phép. Phần cứng máy chủ hạn chế, viết Stored Proc để tối ưu tốc độ.

Nên viết LINQ khi hệ thống phức tạp, việc bảo trì theo thời gian là rất lớn, Viết LINQ hợp lý sẽ chuyển nhiều logic từ server xử lý lên business layer. Giờ đây xu hướng, chúng ta không viết LINQ to SQL thuần túy nữa, mà chúng ta nên sử dụng ADO.net Entity Framework, EF4. EF4 cho phép: 1- Ánh xạ mềm dẻo cấu trúc bảng ở CSDL vào cấu trúc quan hệ, hướng đối tượng của các object entities trong .NET business layer 2- Vẫn có thể chạy, gọi các câu lệnh dynamic SQL và stored procedure để tận dụng những tính năng lập trình đặc biệt của CSDL 3- Giảm bớt số câu lệnh nặng tính thủ tục (procedural code) để Create Read Update Delete dữ liệu. 4- Thể hiện những mối quan hệ phức tạp như: thừa kế... Hẹn bạn một buổi seminar về EF4 tại Hà nội, ở đó bạn có thể hỏi tôi thoải mái về LINQ.....

Nếu so sánh trên cùng một câu truy vấn giữa LINQ và SP hoặc ANSI SQL thì rõ ràng LINQ thua. Nhưng nếu tính trên phương diện cả bài toán thì chưa chắc. Thực tế mình đã và đang làm việc với REPORT, trong một dự án tầm cỡ quốc gia. REPORT có sử dụng cả SP và LINQ, mình thấy rằng có một số REPORT nếu sử dụng SP phải mất đơn vị hàng 1000 dòng code SQL, trong khi sử dụng LINQ chỉ mất đơn vị là 100 dòng code C#. Tương ứng với nó là performance của LINQ còn khá hơn khi dùng SP. Do đó, hầu hết mình đã dùng LINQ thay thế cho SP.

sp chạy nhanh hơn vì nó được biên dịch trước bỏi sql server con khi bạn truy xuất với linq thì linq sẽ biên dịch đoạn mã c# -> t-sql. Vậy đương nhiên là chậm hơn rồi. Tuy nhiên nếu bạn để ý, linq cung cấp các giải pháp khá linh hoạt cho việc lựa chọn sp thay cho việc dùng câu lệnh được biên dịch lúc thực thi. Và cũng hỗ trợ rất tốt việc gọi sp. Đó là chuyển các sp và user function thành 1 phương thức trong lớp data context.

CodeLearn is an online platform that helps users to learn, practice coding skills and join the online coding contests.

Links

Learning

Training

Fights

Information

About Us

Terms of Use

Help

Help

Discussion

Powered by CodeLearn © 2024. All Rights Reserved. rev 2/24/2024 9:44:06 AM

Trong Visual Studio, chọn menu Tools -> NuGet Package Manger -> Package Manger Console. Chạy lệnh bên dưới:

  • Scaffold-DbContext [-Connection] [-Provider] [-OutputDir]

Ví dụ: Scaffold-DbContext "Data Source=LONGNGUYENDH\SQLEXPRESS;Initial Catalog=EFCore;User ID=sa;Password=123;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

The LINQ to SQL allows us to query and modify SQL Server databases by using LINQ syntax. Entity framework is a great ORM shipped by Microsoft which allows us to query and modify RDBMS like SQL Server, Oracle, DB2 MySQL, etc. by using LINQ syntax. Today, EF is widely used by every .NET application to query to database.

However, if you especially want to learn Entity Framework from scratch have a look at the Entity Framework Tutorial. Here we Provided Beginner, Intermediate, and Advanced concepts of Entity Framework. Now let's see the difference between LINQ to SQL and Entity Framework.If we talk about Both technologies they are somewhat similar. Although they provide an ORM framework. But, there are some important differences.

1) SQL Server only:

The LINQ to SQL works only with SQL Server, especially SQL 2005 and above. Also, It works in a limited way with SQL 2000. On the other hand, Entity framework works with most databases like Oracle, Postgres, DB2, MySQL, and plenty more.

2) One-to-One mapping:

The LINQ to SQL is much simpler than Entity Framework. Because it provides an exact one-to-one mapping between the database table and the class. In terms of the Entity framework, there is a many-to-many relation between the database table and the class. A single database table can map to multiple classes. Or a single class can map to multiple tables.

3) Complex types:

Entity framework allows complex types to be defined. For example, consider the Contact table with the telephone number field. The telephone number is a complex type with properties such as international code, telephone number, and extension. Entity Framework can handle such complex types very well than LINQ to SQL. LINQ to SQL has no support for complex types.

4) Model file format:

In terms of model file format, a DBML file represents the model for LINQ to SQL. The model represents the mapping between the classes and the storage tables. For Entity Framework an EDMX file stores the model.

5) Database first approach:

LINQ to SQL always has a Database-first approach. While Entity framework has three approaches: Database first, Model first, and Code first.

1. Database first: Generate model and classes from the database.

2. Model first: Generate database and classes from the model.

3. Code first: Generate database and model from classes.

LINQ to SQL Vs Entity Framework both Differences in a nutshell:

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 a .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 no support for complex types.

It has support for complex types.

It cannot generate a database from the model.

It can generate a database from the 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 the relational tables /views

It allows you to query data using DataContext.

It allows you to query data using EntitySQL, ObjectContext, and DbContext.

It provides a tightly coupled approach.

It provides a loosely coupled approach. Since its code-first approach allows you to use a Dependency Injection pattern which makes it loosely coupled.

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, DB2 MySQL, etc.

Summary:

I hope you will enjoy the LINQ to SQL and EF while playing with the database. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome. Also, Check the below link to learn more about the Entity framework if you have a passion. Enjoy coding...!

Unlock the next level of Entity Framework:

FAQs

Q1. Why LINQ over SQL?

In terms of LINQ, It provides flexibility, familiarity, and developer-friendly syntax, while SQL excels in handling complex relational operations and optimized database performance.

Q2. Why use Entity Framework instead of SQL?

The SQL has better performance and flexibility, but lower ease of use and code maintainability. While Entity Framework Core, on the other hand, has better ease of use, security, and code maintainability, but may not perform as well in some scenarios

Q3. When should I use Entity Framework?

With the Entity Framework, programmers can work at a higher level of abstraction when they deal with data and can create and maintain data-oriented applications with less code.