最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#使用FreeSql生產(chǎn)環(huán)境時(shí)自動(dòng)升級(jí)備份數(shù)據(jù)庫(kù)

 更新時(shí)間:2021年06月22日 08:30:47   作者:一級(jí)碼農(nóng)VIP  
使用FreeSql,包含所有的ORM數(shù)據(jù)庫(kù),都會(huì)存在這樣的問題。在codefirst模式下,根據(jù)代碼自動(dòng)更新數(shù)據(jù)庫(kù),都建議不要在生產(chǎn)環(huán)境使用。因?yàn)槿菀讈G失數(shù)據(jù),本文提供一種自動(dòng)更新數(shù)據(jù)庫(kù)的解決的思路:在判斷需要升級(jí)時(shí),才自動(dòng)升級(jí),同時(shí)升級(jí)前先備份數(shù)據(jù)庫(kù)

項(xiàng)目場(chǎng)景:

使用FreeSql,包含所有的ORM數(shù)據(jù)庫(kù),都會(huì)存在這樣的問題。在codefirst模式下,根據(jù)代碼自動(dòng)更新數(shù)據(jù)庫(kù),都建議不要在生產(chǎn)環(huán)境使用。為什么呢?
其實(shí)不建議使用,主要是根據(jù)代碼自動(dòng)生成數(shù)據(jù)時(shí),極有可能會(huì)造成數(shù)據(jù)的丟失,比如修改字段類型,自動(dòng)更新的結(jié)果可能并不是自己想的。
但是有一些使用場(chǎng)景是需要在生產(chǎn)環(huán)境自動(dòng)升級(jí)的,比如
我們有一個(gè)CS客戶端的產(chǎn)品,客戶本地離線使用,客戶本地部署,數(shù)據(jù)庫(kù)也是本地?cái)?shù)據(jù)庫(kù),版本從1000,迭代到了1100,中間發(fā)布了100個(gè)版本。這中間可能有多次數(shù)據(jù)庫(kù)更改。我們的客戶也可能遍布全國(guó)各地,版本也都不相同??蛻羧绻麤]有新的需求,可能會(huì)一直使用當(dāng)前舊版本,只有在有新的需求,或者想使用新的功能時(shí),才會(huì)升級(jí)版本。所以升級(jí)的時(shí)間也是不確定的,升級(jí)要求客戶安裝新版軟件,運(yùn)行后自動(dòng)升級(jí)。
那就真的不能在生產(chǎn)環(huán)境使用呢?

解決方案:

概要描述:

解決的思路其實(shí)就是自動(dòng)升級(jí),但是在判斷需要升級(jí)時(shí),才自動(dòng)升級(jí),同時(shí)升級(jí)前先備份數(shù)據(jù)庫(kù)。

具體流程
程序內(nèi)每次有數(shù)據(jù)庫(kù)變更,發(fā)布版本時(shí),修改程序內(nèi)對(duì)應(yīng)版本。比如最開始是1000,最新是1100
在數(shù)據(jù)庫(kù)增加SysConfig表,字段包含DbVer表示當(dāng)前數(shù)據(jù)庫(kù)版本號(hào)
在數(shù)據(jù)庫(kù)增加DbLog表,記錄數(shù)據(jù)庫(kù)升級(jí)日志,此項(xiàng)可選
在首次安裝時(shí),檢查數(shù)據(jù)庫(kù)文件不存在,表示首次安裝,首次安裝時(shí)創(chuàng)建SysConfig表和DbLog表,同時(shí)更新SysConfig表DbVer為程序中記錄版本號(hào)。增加DbLog表日志
以后再次運(yùn)行時(shí),先獲取SysConfig表DbVer,判斷與程序中是否一致,
如果數(shù)據(jù)庫(kù)比程序中大,說(shuō)明運(yùn)行低版本的程序,根據(jù)情況可以禁止運(yùn)行。也可以不同步數(shù)據(jù)庫(kù),繼續(xù)運(yùn)行,根據(jù)實(shí)際情況決定。如果對(duì)程序和數(shù)據(jù)庫(kù)一致性要求比較高,可以禁止運(yùn)行。
如果數(shù)據(jù)庫(kù)比程序小,說(shuō)明數(shù)據(jù)庫(kù)需要升級(jí),此時(shí)先備份現(xiàn)有數(shù)據(jù)庫(kù),然后執(zhí)行同步數(shù)據(jù)庫(kù)。

詳細(xì)說(shuō)明:

直接上代碼,比啥都清楚

program.cs文件代碼

using Bonn.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using FreeSql.DataAnnotations;
using WindowsClient.Model;
using System.Reflection;

namespace WindowsClient
{
    static class Program
    {
        /// <summary>
        /// 客戶數(shù)據(jù)庫(kù)路徑
        /// </summary>
        private static string CustDbPath = Application.StartupPath + $"\\數(shù)據(jù)庫(kù)\\cust.db";

        /// <summary>
        /// 數(shù)據(jù)庫(kù)ORM
        /// </summary>
        public static IFreeSql fsql;

        /// <summary>
        /// 服務(wù)器數(shù)據(jù)庫(kù)版本
        /// </summary>
        private static int ServerDbVer = 1000;


        /// <summary>
        /// 應(yīng)用程序的主入口點(diǎn)。
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {

                //數(shù)據(jù)庫(kù)是否存在,用于插入初始數(shù)據(jù),必須在freesql實(shí)例化前判斷,因?yàn)閷?shí)例化時(shí)會(huì)自動(dòng)創(chuàng)建數(shù)據(jù)庫(kù)
                var custDbPathExists = File.Exists(CustDbPath);

                //deebug自動(dòng)同步實(shí)體結(jié)構(gòu)到數(shù)據(jù)庫(kù),release手動(dòng)同步
                bool syncDbStructure = false;
#if DEBUG
                syncDbStructure = true;
#endif

                fsql = new FreeSql.FreeSqlBuilder()
                    .UseConnectionString(FreeSql.DataType.Sqlite, $@"Data Source={CustDbPath}; Pooling=true;Min Pool Size=1")
                    .UseAutoSyncStructure(syncDbStructure) //deebug自動(dòng)同步實(shí)體結(jié)構(gòu)到數(shù)據(jù)庫(kù),release手動(dòng)同步
                    .UseMonitorCommand(cmd => Console.WriteLine($"線程:{cmd.CommandText}\r\n"))
                    .Build(); //請(qǐng)務(wù)必定義成 Singleton 單例模式

                if(syncDbStructure)
                {
                    //主要用于開發(fā)模式下,讓數(shù)據(jù)庫(kù)修改快速生效,不加此句時(shí),只有在用到表時(shí)才會(huì)同步
                    fsql.CodeFirst.SyncStructure(GetTypesByTableAttribute());
                }

                if (custDbPathExists == false)
                {
                    //數(shù)據(jù)庫(kù)文件不存在,表示是首次安裝
                    fsql.CodeFirst.SyncStructure(GetTypesByTableAttribute());
                    var sysConfig = new SysConfig();
                    sysConfig.DbVer = ServerDbVer;
                    var dbResult = fsql.Insert(sysConfig).ExecuteAffrows();
                    if (dbResult <= 0)
                        throw new Exception("初始數(shù)據(jù)庫(kù)失敗。");

                    var row = new DbLog();
                    row.DbVer = ServerDbVer;
                    fsql.Insert(row).ExecuteAffrows();
                }
                int localDbVer = fsql.Select<SysConfig>().First().DbVer;
                if (localDbVer != ServerDbVer)
                {
                    //數(shù)據(jù)庫(kù)版本不一樣,需要升級(jí)
                    //備份數(shù)據(jù)庫(kù)
                    File.Copy(CustDbPath, Application.StartupPath + $"\\數(shù)據(jù)庫(kù)\\cust{DateTime.Now:yyyyMMddHHmmss}.db");
                    //升級(jí)數(shù)據(jù)庫(kù)
                    fsql.CodeFirst.SyncStructure(GetTypesByTableAttribute());
                    var row = new DbLog();
                    row.DbVer = ServerDbVer;
                    fsql.Insert(row).ExecuteAffrows();
                }

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FrmMain());
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "出錯(cuò)啦", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public static Type[] GetTypesByTableAttribute()
        {
            List<Type> tableAssembies = new List<Type>();
            foreach (Type type in Assembly.GetAssembly(typeof(IEntity)).GetExportedTypes())
            {
                foreach (Attribute attribute in type.GetCustomAttributes())
                {
                    if (attribute is TableAttribute tableAttribute)
                    {
                        if (tableAttribute.DisableSyncStructure == false)
                        {
                            tableAssembies.Add(type);
                        }
                    }
                }
            };
            return tableAssembies.ToArray();
        }
    }
}

SysConfig.cs

using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsClient.Model
{
    /// <summary>
    /// 
    /// </summary>
    [Table(Name = "sys_config")]
    public class SysConfig : IEntity
    {
        /// <summary>
        /// 主鍵
        /// </summary>
        [Column(Name = "id", IsIdentity = true, IsPrimary = true)]
        public int Id { get; set; }

        /// <summary>
        /// 主鍵
        /// </summary>
        [Column(Name = "dbVer")]
        public int DbVer { get; set; }

        /// <summary>
        /// 創(chuàng)建時(shí)間
        /// </summary>
        [Column(ServerTime = DateTimeKind.Local, CanUpdate = false)]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 修改時(shí)間
        /// </summary>
        [Column(ServerTime = DateTimeKind.Local, CanUpdate = true)]
        public DateTime UpdateTime { get; set; }

    }
}

DbLog.cs

using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsClient.Model
{
    /// <summary>
    /// 
    /// </summary>
    [Table(Name = "db_log")]
    public class DbLog : IEntity
    {
        /// <summary>
        /// 主鍵
        /// </summary>
        [Column(Name = "id", IsIdentity = true, IsPrimary = true)]
        public int Id { get; set; }

        /// <summary>
        /// 主鍵
        /// </summary>
        [Column(Name = "dbVer")]
        public int DbVer { get; set; }

        /// <summary>
        /// 創(chuàng)建時(shí)間
        /// </summary>
        [Column(ServerTime = DateTimeKind.Local, CanUpdate = false)]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 修改時(shí)間
        /// </summary>
        [Column(ServerTime = DateTimeKind.Local, CanUpdate = true)]
        public DateTime UpdateTime { get; set; }
    }
}

總結(jié):

以前是手寫的SQL語(yǔ)句,現(xiàn)在用FreeSql確實(shí)方便多了。

以上就是c#使用FreeSql生產(chǎn)環(huán)境自動(dòng)升級(jí)備份數(shù)據(jù)庫(kù)的詳細(xì)內(nèi)容,更多關(guān)于c# 用FreeSql自動(dòng)升級(jí)備份數(shù)據(jù)庫(kù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

凤山县| 邯郸县| 金昌市| 新沂市| 新沂市| 呼玛县| 襄垣县| 临安市| 五寨县| 宁南县| 龙川县| 柳林县| 闻喜县| 彭山县| 青冈县| 禄丰县| 北安市| 临湘市| 滁州市| 陆丰市| 虎林市| 桃江县| 梁河县| 岳普湖县| 天柱县| 新竹市| 伊金霍洛旗| 益阳市| 神木县| 镇宁| 宁海县| 章丘市| 清丰县| 策勒县| 芦溪县| 抚远县| 白山市| 灵台县| 岑巩县| 宿松县| 康定县|