dotnetcore / dotnetcore/FreeSql

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

Open
#2,268 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.4k
Forks
910
PR merge metrics
No merged PRs in 30d

Description

在文档
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 sqliteLazy = new Lazy(() => {
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;
}
}
```
我希望在文档中提出此问题,并给出第三种用法。

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the linked guide and wiki sections on connecting to FreeSql, then compare the Lazy-based example with the static-constructor example in the issue. Verify whether the reported ThreadAbortException is related to initialization timing before proposing a third usage. Done means the documentation presents an agreed, reproducible recommendation and its relevant caveat.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sqlite
Domain
databases, documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.