-
Notifications
You must be signed in to change notification settings - Fork 857
常见问题
方法一:UseMonitorCommand + UseNoneCommandParameter
static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, "...")
.UseMonitorCommand(cmd => Console.WriteLine($"线程:{cmd.CommandText}\r\n"))
.UseNoneCommandParameter(true)
.Build();
方法二:Aop.CurdBefore/CurdAfter
fsql.Aop.CurdAfter += (s, e) =>
{
if (e.ElapsedMilliseconds > 200)
Console.WriteLine($"线程:{e.Sql}\r\n");
};
默认情况 c# 枚举会映射为 MySql Enum 类型,如果想映射为 int 在 FreeSqlBuilder Build 之后执行以下 Aop 统一处理:
fsql.Aop.ConfigEntityProperty += (s, e) =>
{
if (e.Property.PropertyType.IsEnum)
e.ModifyResult.MapType = typeof(int);
};
https://github.com/dotnetcore/FreeSql/issues/44
//直接查询
fsql.Ado.Query<T>(sql);
//嵌套一层做二次查询
fsql.Select<T>().WithSql(sql).Page(1, 10).ToList();
//使用内存数据
fsql.Select<T>().WithMemory(list).ToList();
https://github.com/dotnetcore/FreeSql/discussions/1080
https://github.com/dotnetcore/FreeSql/discussions/1079
https://github.com/dotnetcore/FreeSql/discussions/1081
有条件的同学直接试试 FreeSql.Provider.SqliteCore 包,使用的就是 Microsoft.Data.Sqlite 驱动.
1.添加包
<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.0.100" />
<PackageReference Include = "Microsoft.Data.Sqlite" Version="6.0.3" />
2.代码
var _database = new Microsoft.Data.Sqlite.SqliteConnection($"Data Source=document.db");
var fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionFactory(FreeSql.DataType.Sqlite, () => _database, typeof(FreeSql.Sqlite.SqliteProvider<>))
.UseAutoSyncStructure(true)
.UseNoneCommandParameter(true) //必须开启,因为Microsoft.Data.Sqlite内插处理有bug
.UseMonitorCommand(cmd => Console.Write(cmd.CommandText))
.Build();
using System.Data.SQLite;
var dbpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "sqliteSample.db");
var _database = new SQLiteConnection($"Data Source={dbpath}");
var fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionFactory(FreeSql.DataType.Sqlite, () => _database, typeof(FreeSql.Sqlite.SqliteProvider<>))
.Build();
Xamarin Forms,代码较多 主程序,接口获取rovider,各个平台自己实现.
if (Device.RuntimePlatform == Device.iOS || Device.RuntimePlatform == Device.Android)
{
fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionFactory(FreeSql.DataType.Sqlite, () => DependencyService.Get<ISQLite>().GetConnectionSqlite("document"), typeof(FreeSql.Sqlite.SqliteProvider<>))
.UseNoneCommandParameter(true)
.Build();
}
请尝试:
1.连接字符串里加入 "Encrypt=True; TrustServerCertificate=True;"
2.使用FreeSql.Provider.SqlServerForSystem替换FreeSql.Provider.SqlServer
深入讨论请转到 https://github.com/dotnetcore/FreeSql/issues/992#issuecomment-1005305027