mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-09-18 01:15:34 +08:00
update readme
This commit is contained in:
@@ -7,6 +7,7 @@ using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace dbcontext_01
|
||||
{
|
||||
@@ -19,49 +20,22 @@ namespace dbcontext_01
|
||||
Fsql = new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document2.db;Pooling=true;Max Pool Size=10")
|
||||
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=10")
|
||||
|
||||
//.UseConnectionString(DataType.MySql, "Data Source=192.168.164.10;Port=33061;User ID=root;Password=123456;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=5")
|
||||
//.UseSlave("Data Source=192.168.164.10;Port=33062;User ID=root;Password=123456;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=5")
|
||||
|
||||
//.UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=10")
|
||||
//.UseSyncStructureToUpper(true)
|
||||
|
||||
.UseAutoSyncStructure(true)
|
||||
.UseLazyLoading(true)
|
||||
.UseNoneCommandParameter(true)
|
||||
|
||||
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText),
|
||||
(cmd, log) => Trace.WriteLine(log)
|
||||
)
|
||||
.UseMonitorCommand(cmd => { }, (cmd, log) => Trace.WriteLine(log))
|
||||
.Build();
|
||||
Fsql.Aop.SyncStructureBefore += (s, e) =>
|
||||
{
|
||||
Console.WriteLine(e.Identifier + ": " + string.Join(", ", e.EntityTypes.Select(a => a.FullName)));
|
||||
};
|
||||
Fsql.Aop.SyncStructureAfter += (s, e) =>
|
||||
{
|
||||
Console.WriteLine(e.Identifier + ": " + string.Join(", ", e.EntityTypes.Select(a => a.FullName)) + " " + e.ElapsedMilliseconds + "ms\r\n" + e.Exception?.Message + e.Sql);
|
||||
};
|
||||
|
||||
Fsql.Aop.CurdBefore += (s, e) =>
|
||||
{
|
||||
Console.WriteLine(e.Identifier + ": " + e.EntityType.FullName + ", " + e.Sql);
|
||||
};
|
||||
Fsql.Aop.CurdAfter += (s, e) =>
|
||||
{
|
||||
Console.WriteLine(e.Identifier + ": " + e.EntityType.FullName + " " + e.ElapsedMilliseconds + "ms, " + e.Sql);
|
||||
CurdAfterLog.Current.Value?.Sb.AppendLine($"{Thread.CurrentThread.ManagedThreadId}: {e.EntityType.FullName} {e.ElapsedMilliseconds}ms, {e.Sql}");
|
||||
};
|
||||
|
||||
Fsql2 = new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document222.db;Pooling=true;Max Pool Size=10")
|
||||
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=10")
|
||||
.UseAutoSyncStructure(true)
|
||||
.UseLazyLoading(true)
|
||||
|
||||
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText),
|
||||
(cmd, log) => Trace.WriteLine(log)
|
||||
)
|
||||
.Build<long>();
|
||||
}
|
||||
|
||||
enum MySql { }
|
||||
@@ -69,20 +43,14 @@ namespace dbcontext_01
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
public static IFreeSql Fsql { get; private set; }
|
||||
public static IFreeSql<long> Fsql2 { get; private set; }
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddControllersWithViews();
|
||||
|
||||
services.AddSingleton<IFreeSql>(Fsql);
|
||||
services.AddSingleton<IFreeSql<long>>(Fsql2);
|
||||
services.AddFreeDbContext<SongContext>(options => options.UseFreeSql(Fsql));
|
||||
|
||||
|
||||
var sql1 = Fsql.Update<Song>(1).Set(a => a.Id + 10).ToSql();
|
||||
var sql2 = Fsql.Update<Song>(1).Set(a => a.Title + 10).ToSql();
|
||||
var sql3 = Fsql.Update<Song>(1).Set(a => a.Create_time.Value.AddHours(1)).ToSql();
|
||||
services.AddScoped<CurdAfterLog>();
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app)
|
||||
@@ -97,4 +65,20 @@ namespace dbcontext_01
|
||||
app.UseEndpoints(a => a.MapControllers());
|
||||
}
|
||||
}
|
||||
|
||||
public class CurdAfterLog : IDisposable
|
||||
{
|
||||
public static AsyncLocal<CurdAfterLog> Current = new AsyncLocal<CurdAfterLog>();
|
||||
public StringBuilder Sb { get; } = new StringBuilder();
|
||||
|
||||
public CurdAfterLog()
|
||||
{
|
||||
Current.Value = this;
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
Sb.Clear();
|
||||
Current.Value = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user