CÔNG NGHỆ THÔNG TIN >> BÀI VIẾT CHỌN LỌC

Sinh ra entity class từ table sử dụng thủ tục trong SQL Server

Đăng lúc: 11:28 AM - 21/09/2023 bởi Charles Chung - 509

Trong bài viết này tôi sẽ hướng dẫn các bạn cách viết thủ tục (Stored Procedure) để sinh ra entity class từ một bảng bất kỳ trong SQL Server.

1. Giới thiệu

SQL Server là một hệ quản trị cơ sở dữ liệu quan hệ khá phổ biến, các ứng dụng trên nền tảng .NET thường làm việc với cơ sở dữ liệu SQL Server. Nếu các bạn sử dụng các công nghệ như Entity Framework hoặc Linq to Class để làm việc với SQL Server thì việc sinh ra các entity class trong .NET sẽ rất dễ ràng bằng các sử dụng công cụ Visual Studio. Tuy nhiên nếu bạn sử dụng ADO.NET hoặc Dapper thì bạn phải tự viết các entity class để map với các table trong cơ sở dữ liệu, và nếu cơ sở dữ liệu có hàng 100 bảng thì chắc chắn sẽ rất vất vả, mất thời gian. Trong bài viết này tôi sẽ hướng dẫn các bạn tạo thủ tục trong SQL Server để xử lý công việc này.

2. Những kiến thức cần biết khi đọc code

  • Bảng sys.columns sẽ chứa danh sách các cột của mọi bảng trong SQL Server
  • Bảng sys.types sẽ chứa các kiểu dữ liệu trong SQL Server
  • Hàm object_id('table_name') sẽ trả về mã số của bảng

3. Source code tham khảo

/*

Tạo thủ tục sinh ra 1 entity class C#

với tham số truyền vào là 1 tên bảng trong SQL Server

*/

CREATE PROC sp_GenerateClassCsharp

@TableName varchar(200), --Tên bảng

@Result varchar(max) output --Chuỗi đầu ra chứa nội dung class

AS

BEGIN

set @Result= 'public class ' + @TableName + '{'

select @Result = @Result + 'public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }'

from

(

select replace(col.name, ' ', '_') ColumnNamecolumn_id ColumnId,

case typ.name

when 'bigint' then 'long'

when 'binary' then 'byte[]'

when 'bit' then 'bool'

when 'char' then 'string'

when 'date' then 'DateTime'

when 'datetime' then 'DateTime'

when 'datetime2' then 'DateTime'

when 'datetimeoffset' then 'DateTimeOffset'

when 'decimal' then 'decimal'

when 'float' then 'double'

when 'image' then 'byte[]'

when 'int' then 'int'

when 'money' then 'decimal'

when 'nchar' then 'string'

when 'ntext' then 'string'

when 'numeric' then 'decimal'

when 'nvarchar' then 'string'

when 'real' then 'double'

when 'smalldatetime' then 'DateTime'

when 'smallint' then 'short'

when 'smallmoney' then 'decimal'

when 'text' then 'string'

when 'time' then 'TimeSpan'

when 'timestamp' then 'DateTime'

when 'tinyint' then 'byte'

when 'uniqueidentifier' then 'Guid'

when 'varbinary' then 'byte[]'

when 'varchar' then 'string'

else 'UNKNOWN_' + typ.name

end ColumnType,

case

when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier')

then '?'

else ''

end NullableSign

from sys.columns col

join sys.types typ

on col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id

where object_id = object_id(@TableName)) Table_Column order by Table_Column.ColumnId

set @Result = @Result  + '}'

END

 

--Thực thi thủ tục trong SQL Server

declare @result varchar(max)

exec sp_GenerateClassCsharp 'Category', @result output

print @result

 

4. Kết quả thực thi

 

 

thay lời cảm ơn!

QUẢNG CÁO - TIẾP THỊ