Skip to content

【讨论】希望更新文档 ,推荐第三种 IFreeSql 的用法 #2268

Description

@ping9719

在文档
https://freesql.net/guide/#%E5%A6%82%E4%BD%95%E6%8E%A5%E5%85%A5
https://github.com/dotnetcore/FreeSql/wiki/%e5%85%a5%e9%97%a8#%E5%A3%B0%E6%98%8E
中使用的代码

public class DB
{
    static Lazy<IFreeSql> sqliteLazy = new Lazy<IFreeSql>(() => {
        var fsql = new FreeSql.FreeSqlBuilder()
            .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db")
            .UseMonitorCommand(cmd => Console.WriteLine($"Sql:{cmd.CommandText}"))//监听SQL语句
            .UseAutoSyncStructure(true)  
            .Build();
        return fsql;
    });
    public static IFreeSql Sqlite => sqliteLazy.Value;
}

我发现在偶发情况下会出现错误 System.Threading.ThreadAbortException:“System error.” 我反复排查可能是因为在使用的时候没有预加载。
https://www.qianwen.com/share/chat/852bcd850d95462485404dbb56f9c149

我将来代码更改为传统的静态类就没有出现问题了

    public static class DataDb
    {
        public static IFreeSql db;
        static DataDb()
        {
            var fsql = new FreeSqlBuilder()
                .UseMonitorCommand(cmd => { })
                .UseAdoConnectionPool(true)
                .UseConnectionString(DataType.Sqlite, $"Data Source={Path.Combine(LocalStatic.DbDir, "data.db3")};Version=3;journal_mode=WAL;")
                .UseAutoSyncStructure(false) 
                .Build();

            fsql.Aop.CommandAfter += (s, e) =>
            {
                if (e.Exception != null)
                {
                    Logs.Write($"{nameof(DataDb)}\r\nMessage:{e.Exception.Message}\r\nStackTrace:{e.Exception.StackTrace}\r\nCommandText:{e.Command.CommandText}\r\n", LogsType.DbError);
                }
            };
            db = fsql;
        }
    }

我希望在文档中提出此问题,并给出第三种用法。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions