亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

為ABP框架配置數(shù)據(jù)庫

 更新時間:2022年02月23日 11:30:07   作者:癡者工良  
這篇文章介紹了為ABP框架配置數(shù)據(jù)庫的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

在 AbpBase.Database 中,通過 Nuget 添加以下幾個庫:

版本都是 1.9.0-preview0917,你可以使用最新版本的。

Freesql									
FreeSql.Provider.Sqlite
FreeSql.Provider.SqlServer
FreeSql.Provider.MySql

創(chuàng)建標準的 EFCore 數(shù)據(jù)庫上下文

在 ABP 中,EFCore 上下文類需要繼承 AbpDbContext<T>,整體編寫方法跟繼承 DbContext<T> 一致 ,接下來我們將一步步來講解在 AbpBase 中如何添加 EFCore 功能。

連接字符串

ABP 中,可以在上下文類加上一個 ConnectionStringName 特性,然后在配置服務(wù)時,ABP 會自動為其配置連接字符串。

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

Default 是一個標識,你也可以填寫其他字符串標識。

定義隔離的上下文

首先,我們在 AbpBase.Database 模塊中,創(chuàng)建兩個文件夾:

BaseData
ExtensionData

BaseData 目錄用來存放基礎(chǔ)表結(jié)構(gòu)的上下文,ExtensionData 用來存放可能會拓展或者經(jīng)常變動的表結(jié)構(gòu)。

在 BaseData 中創(chuàng)建一個 AbpBaseDataContext 類,其內(nèi)容如下:

using Microsoft.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;

namespace AbpBase.Database
{
    /// <summary>
    /// 上下文
    /// <para>這部分用于定義和配置基礎(chǔ)表的映射</para>
    /// </summary>
    [ConnectionStringName("Default")]
    public partial class AbpBaseDataContext : AbpDbContext<AbpBaseDataContext>
    {

        #region 定義 DbSet<T>

        #endregion


        public AbpBaseDataContext(DbContextOptions<AbpBaseDataContext> options)
    : base(options)
        {
        }

        /// <summary>
        /// 定義映射
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            #region 定義 映射

            #endregion

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}

在 ExtensionData 中也創(chuàng)建一個相同的 AbpBaseDataContext 類,其內(nèi)容如下:

using Microsoft.EntityFrameworkCore;

namespace AbpBase.Database
{

    public partial class AbpBaseDataContext
    {
        #region 定義 DbSet<T>

        #endregion

        /// <summary>
        /// 定義映射
        /// </summary>
        /// <param name="modelBuilder"></param>
        partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
        {

        }
    }
}

分部類,前者用于定義那些非?;A(chǔ)的,程序核心的實體(表)以及映射。而后者定義后續(xù)可能多次修改的,設(shè)計時感覺有設(shè)計余地的。

多數(shù)據(jù)庫支持和配置

這里我們將對上下文進行配置和注入,使得程序能夠支持多數(shù)據(jù)庫。

在 AbpBase.Domain.Shared 項目中,創(chuàng)建一個枚舉,其內(nèi)容如下:

namespace AbpBase.Domain.Shared
{
    public enum AbpBaseDataType
    {
        Sqlite = 0,
        Mysql = 1,
        Sqlserver = 2

        // 其他數(shù)據(jù)庫
    }
}

再創(chuàng)建一個 WholeShared 類,其內(nèi)容如下:

namespace AbpBase.Domain.Shared
{
    /// <summary>
    /// 全局共享內(nèi)容
    /// </summary>
    public static class WholeShared
    {
        // 數(shù)據(jù)庫連接屬性可以自行在配置文件中定義,這里寫固定的,只是為了演示

        /// <summary>
        /// 數(shù)據(jù)庫連接字符串
        /// </summary>
        public static readonly string SqlConnectString = "";

        /// <summary>
        /// 要使用的數(shù)據(jù)庫類型
        /// </summary>
        public static readonly AbpBaseDataType DataType = AbpBaseDataType.Sqlite;
    }
}

然后我們在 AbpBaseDatabaseModule 模塊中的 ConfigureServices 函數(shù)里面添加依賴注入:

context.Services.AddAbpDbContext<AbpBaseDataContext>();

這里不需要配置數(shù)據(jù)庫連接字符串,后面可以通過 ABP 的一些方法來配置。

配置上下文連接字符串

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

配置多數(shù)據(jù)庫支持:

            FreeSql.DataType dataType = default;

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

這樣就完成了對 EFCore 的多數(shù)據(jù)庫配置了。

下面我們來使用類似的方法配置 Freesql。

Freesql 配置服務(wù)

首先,F(xiàn)reesql 里面有多種配置方式,例如 DbContext,讀者可以到 Wiki 去學(xué)習(xí) Freesql

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

筆者這里使用的是 “非正規(guī)” 的設(shè)計方式,哈哈哈哈。

在 BaseData 目錄中,創(chuàng)建一個 FreesqlContext 類,其內(nèi)容如下:

using FreeSql.Internal;
using System;
using System.Collections.Generic;
using System.Text;

namespace AbpBase.Database
{
    /// <summary>
    /// Freesql 上下文
    /// </summary>
    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) // 自動同步實體結(jié)構(gòu)到數(shù)據(jù)庫,生產(chǎn)環(huán)境禁止使用!

                .Build();
            OnModelCreating(Freesql_Instance);
        }

        private static void OnModelCreating(IFreeSql freeSql)
        {


            OnModelCreatingPartial(freeSql);
        }
    }
}

ExtensionData 目錄中,創(chuàng)建 FreesqlContext 類 如下:

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);

        }

        /// <summary>
        /// 同步結(jié)構(gòu)到數(shù)據(jù)中
        /// </summary>
        /// <param name="codeFirst"></param>
        private static void SyncStruct(ICodeFirst codeFirst)
        {
            //  codeFirst.SyncStructure(typeof(user));
        }
    }
}

然后在 AbpBaseDatabaseModule 的 ConfigureServices 函數(shù)中添加注入服務(wù):

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

通過以上步驟,我們的 ABP 就可以支持多數(shù)據(jù)庫了,EFCore + Freesql,并且將將表分級隔離維護。

到此這篇關(guān)于為ABP框架配置數(shù)據(jù)庫的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • ASP.NET?Core中的wwwroot文件夾

    ASP.NET?Core中的wwwroot文件夾

    這篇文章介紹了ASP.NET?Core中的wwwroot文件夾,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • C#Web應(yīng)用程序入門經(jīng)典學(xué)習(xí)筆記之一

    C#Web應(yīng)用程序入門經(jīng)典學(xué)習(xí)筆記之一

    C#Web應(yīng)用程序入門經(jīng)典學(xué)習(xí)筆記之一...
    2006-08-08
  • ASP.NET?MVC框架簡介

    ASP.NET?MVC框架簡介

    這篇文章介紹了ASP.NET?MVC框架,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • WPF框架Prism中ViewModelLocator用法介紹

    WPF框架Prism中ViewModelLocator用法介紹

    這篇文章介紹了WPF框架Prism中ViewModelLocator的用法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • .NET?Core使用flyfire.CustomSerialPort實現(xiàn)Windows/Linux跨平臺串口通訊

    .NET?Core使用flyfire.CustomSerialPort實現(xiàn)Windows/Linux跨平臺串口通訊

    本文詳細講解了.NET?Core使用flyfire.CustomSerialPort實現(xiàn)Windows/Linux跨平臺串口通訊的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • 頁面包含的處理

    頁面包含的處理

    頁面包含的處理...
    2006-08-08
  • .Net結(jié)構(gòu)型設(shè)計模式之代理模式(Proxy)

    .Net結(jié)構(gòu)型設(shè)計模式之代理模式(Proxy)

    這篇文章介紹了.Net結(jié)構(gòu)型設(shè)計模式之代理模式(Proxy),文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • WPF框架之Prism介紹

    WPF框架之Prism介紹

    這篇文章介紹了WPF的Prism框架,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • ASP.NET?Core?MVC中Required與BindRequired用法與區(qū)別介紹

    ASP.NET?Core?MVC中Required與BindRequired用法與區(qū)別介紹

    這篇文章介紹了ASP.NET?Core?MVC中Required與BindRequired用法與區(qū)別,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • ASP.NET?Core中的Configuration配置二

    ASP.NET?Core中的Configuration配置二

    這篇文章介紹了ASP.NET?Core中的Configuration配置,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04

最新評論