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

How to configure the database for the ABP framework

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to configure the database for the ABP framework", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to configure the database for the ABP framework.

In AbpBase.Database, add the following libraries through Nuget:

The versions are all 1.9.0-preview0917, and you can use the latest version.

Freesql FreeSql.Provider.SqliteFreeSql.Provider.SqlServerFreeSql.Provider.MySql creates a standard EFCore database context

In ABP, the EFCore context class needs to inherit AbpDbContext, and the overall writing method is the same as inheriting DbContext. Next, we will explain how to add EFCore functionality in AbpBase step by step.

Connection string

In ABP, you can add a ConnectionStringName feature to the context class, and then when you configure the service, ABP automatically configures the connection string for it.

[ConnectionStringName ("Default")] public partial class DatabaseContext: AbpDbContext

Default is a logo, you can also fill in other string identifiers.

Define the context of isolation

First, we create two folders in the AbpBase.Database module:

BaseDataExtensionData

The BaseData directory is used to store the context of the underlying table structure, and the ExtensionData is used to store table structures that may be extended or change frequently.

Create an AbpBaseDataContext class in BaseData with the following contents:

Using Microsoft.EntityFrameworkCore;using Volo.Abp.Data;using Volo.Abp.EntityFrameworkCore Namespace AbpBase.Database {/ context / / this section is used to define and configure the mapping of the underlying table / [ConnectionStringName ("Default")] public partial class AbpBaseDataContext: AbpDbContext {# region definition DbSet # endregion public AbpBaseDataContext (DbContextOptions options): base (options) {} / / / define mapping / protected override void OnModelCreating (ModelBuilder modelBuilder) {# region definition mapping # endregion OnModelCreatingPartial (modelBuilder) } partial void OnModelCreatingPartial (ModelBuilder modelBuilder);}}

Create the same AbpBaseDataContext class in ExtensionData with the following contents:

Using Microsoft.EntityFrameworkCore;namespace AbpBase.Database {public partial class AbpBaseDataContext {# region definition DbSet # endregion / define mapping / partial void OnModelCreatingPartial (ModelBuilder modelBuilder) {}

Partial classes, the former are used to define very basic, program-core entities (tables) and mappings. The latter definition may be modified many times later, and it feels like there is room for design at the time of design.

Multi-database support and configuration

Here we will configure and inject the context so that the program can support multiple databases.

In the AbpBase.Domain.Shared project, create an enumeration with the following contents:

Namespace AbpBase.Domain.Shared {public enum AbpBaseDataType {Sqlite = 0, Mysql = 1, Sqlserver = 2 / / other databases}}

Create another WholeShared class with the following contents:

Namespace AbpBase.Domain.Shared {/ Global shared content / public static class WholeShared {/ / Database connection properties can be defined in the configuration file, which is fixed here, just to demonstrate / database connection string / public static readonly string SqlConnectString = "" / the type of database to be used / public static readonly AbpBaseDataType DataType = AbpBaseDataType.Sqlite;}}

Then we add dependency injection to the ConfigureServices function in the AbpBaseDatabaseModule module:

Context.Services.AddAbpDbContext ()

There is no need to configure the database connection string, which can be configured later through some of the methods of ABP.

Configure context connection string

String connectString = default; Configure (options = > {connectString = WholeShared.SqlConnectString; options.ConnectionStrings.Default = connectString;})

Configure multi-database support:

FreeSql.DataType dataType = default; Configure (options = > {switch (WholeShared.DataType) {case AbpBaseDataType.Sqlite: options.UseSqlite (); dataType = FreeSql.DataType.Sqlite; break; case AbpBaseDataType.Mysql: options.UseMySQL (); dataType = FreeSql.DataType.MySql Break; case AbpBaseDataType.Sqlserver: options.UseSqlServer (); dataType = FreeSql.DataType.SqlServer; break;}})

This completes the multi-database configuration of EFCore.

Let's configure Freesql using a similar method.

Freesql configuration Service

First of all, there are many configuration methods in Freesql, such as DbContext. Readers can go to Wiki to learn Freesql:

Https://github.com/dotnetcore/FreeSql/wiki/%E5%85%A5%E9%97%A8

What the author uses here is an "informal" design method, ha.

In the BaseData directory, create a FreesqlContext class with the following contents:

Using FreeSql.Internal;using System;using System.Collections.Generic;using System.Text;namespace AbpBase.Database {/ Freesql context / public partial class FreesqlContext {public static IFreeSql FreeselInstance = > Freesql_Instance; private static IFreeSql Freesql_Instance Public static void Init (string connectStr, FreeSql.DataType dataType = FreeSql.DataType.Sqlite) {Freesql_Instance = new FreeSql.FreeSqlBuilder () .UseNameConvert (NameConvertType.PascalCaseToUnderscore) .UseConnectionString (dataType, connectStr) / / .UseAutoSyncStructure (true) / / automatically synchronizes the physical structure to the database, which is prohibited in the production environment! .build (); OnModelCreating (Freesql_Instance);} private static void OnModelCreating (IFreeSql freeSql) {OnModelCreatingPartial (freeSql);}

In the ExtensionData directory, create the FreesqlContext class as follows:

Using FreeSql;using System;using System.Collections.Generic;using System.Text;namespace AbpBase.Database {public partial class FreesqlContext {private static void OnModelCreatingPartial (IFreeSql freeSql) {var modelBuilder = freeSql.CodeFirst; SyncStruct (modelBuilder) } / private static void SyncStruct (ICodeFirst codeFirst) {/ / codeFirst.SyncStructure (typeof (user));}

Then add the injection service to the ConfigureServices function of AbpBaseDatabaseModule:

FreesqlContext.Init (connectString, dataType); context.Services.AddSingleton (typeof (IFreeSql), FreesqlContext.FreeselInstance); context.Services.AddTransient (typeof (FreesqlContext), typeof (FreesqlContext))

Through the above steps, our ABP can support multiple databases, EFCore + Freesql, and the tables will be graded and isolated for maintenance.

At this point, I believe you have a deeper understanding of "how to configure the database for the ABP framework". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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