Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

ABP introduces the method used in the creation of SqlSugar framework

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the introduction of ABP SqlSugar framework to create the use of related knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that everyone after reading this ABP introduction of SqlSugar framework to create the method of article will have a harvest, let's take a look at it.

A new class library

To make the code clear, I built a new class library. Introduced SqlSugar framework package, 2 warehousing classes, 1 DbContext

Declare entity [SugarTable ("BasBloodLevel")] public class BasBloodLevel {[SugarColumn (IsPrimaryKey = true, IsIdentity = true)] public int Id {get; set;} public string Code {get; set;} two basic warehouses

Implement basic warehousing IBaseRepository and BaseRepository first

/ Base class interface, other APIs inherit this interface / public interface IBaseRepository where TEntity: class {/ query / Task QueryByID (object objId) according to ID / / add / Task Add (TEntity model); / modify / Task Update (TEntity model) / delete / Task DeleteByIds (object [] ids) } / Base class implementation / public class BaseRepository: DbContext, IBaseRepository where TEntity: class, new () {/ write entity data / public async Task Add (TEntity model) {/ / note here is that If Task.Run () is used, the sql statement log cannot be recorded and changed to the following / / var I = await Task.Run (() = > Db.Insertable (model) .log ()) Var I = await Db.Insertable (model). ExecuteCommandAsync (); return I > 0;} / public async Task DeleteByIds (object [] ids) {var I = await Db.Deleteable (). In (ids). ExecuteCommandAsync (); return I > 0 Query a piece of data according to ID / public async Task QueryByID (object objId) {return await Db.Queryable (). InSingleAsync (objId); / update entity data / public async Task Update (TEntity model) {/ / this way will be conditional on primary key var I = await Db.Updateable (model). ExecuteCommandAsync (); return I > 0 }} three realize the DB of SqlSugar

The ConnectionString address here, we can read the configuration file of the ABP framework directly, but I wrote it directly for convenience.

Public class DbContext where T: class, new () {public DbContext () {Db = new SqlSugarClient (new ConnectionConfig () {/ / Database address) We can read the configuration file of the ABP framework directly, but for convenience I wrote ConnectionString = "Server=****; Database=****; Uid=sa; Pwd=****;MultipleActiveResultSets=true" directly. ", DbType = DbType.SqlServer, InitKeyType = InitKeyType.Attribute,// read primary key and self-increment column information from properties IsAutoCloseConnection = true,// turn on automatic release mode}) / / Modal code is used to print SQL Db.Aop.OnLogExecuting = (sql, pars) > {Console.WriteLine (sql + "\ r\ n" + Db.Utilities.SerializeObject (pars.ToDictionary (it = > it.ParameterName, it = > it.Value)); Console.WriteLine ();};} / / Note: cannot be written as static public SqlSugarClient Db / / used to handle transactional multi-table queries and complex operations public SimpleClient CurrentDb {get {return new SimpleClient (Db);}} / / data public SimpleClient BasBloodLevelDb {get {return new SimpleClient (Db);}} / / common operations used to process User tables} 4 implement dependency injection

So we can use it globally.

[DependsOn (typeof (AbpZeroCoreModule))] public class Module: AbpModule {public override void Initialize () {IocManager.Register (typeof (IBaseRepository), typeof (BaseRepository), DependencyLifeStyle.Singleton); / / dependency injection assembly IocManager.RegisterAssemblyByConvention (typeof (Module). GetAssembly ());}}

Since you are implementing dependency injection, you must initialize this class to trigger injection. I choose to add it to the EF layer so that it does not affect the initialization of the original EF layer.

5. Application layer usage

Refer directly to the corresponding IBaseRepository repository

Public class BasBloodBreedAppService: BloodTestLibSystemAppServiceBase,IApplicationService {private IBaseRepository _ baseRepository {get; set;} public BasBloodBreedAppService (IBaseRepository baseRepository) {_ baseRepository = baseRepository;} public async Task GetBase () {var ce=await _ baseRepository.QueryByID (1); return ce;}}

Prove that I am successful.

This is the end of the article on "ABP introduces the method of creating SqlSugar framework". Thank you for reading! I believe you all have a certain understanding of the knowledge of "ABP introduces the method used to create the SqlSugar framework". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report