update readme

This commit is contained in:
28810
2020-03-09 19:25:45 +08:00
parent b647d02308
commit 9f0e28bcac
3 changed files with 44 additions and 69 deletions

View File

@ -14,13 +14,12 @@ namespace dbcontext_01.Controllers
IFreeSql _orm;
SongContext _songContext;
public ValuesController(SongContext songContext,
IFreeSql orm1, IFreeSql orm2,
IFreeSql<long> orm3
)
CurdAfterLog _curdLog;
public ValuesController(SongContext songContext, IFreeSql orm1, CurdAfterLog curdLog)
{
_songContext = songContext;
_orm = orm1;
_curdLog = curdLog;
}
@ -230,7 +229,7 @@ namespace dbcontext_01.Controllers
var item22 = await _orm.Select<Song>().Where(a => a.Id == id).FirstAsync();
var item33 = await _orm.Select<Song>().Where(a => a.Id > id).ToListAsync();
return item22.Id.ToString();
return item22.Id.ToString() + "\r\n\r\n" + _curdLog.Sb.ToString();
}
// GET api/values/5
@ -240,6 +239,14 @@ namespace dbcontext_01.Controllers
return _orm.Select<Song>().Where(a => a.Id == id).First();
}
[HttpGet("get{id}")]
public ActionResult<string> Get2(int id)
{
var item1 = _orm.Select<Song>().Where(a => a.Id == id).First();
var item2 = _orm.Select<Song>().Where(a => a.Id == id).First();
return _curdLog.Sb.ToString();
}
// POST api/values
[HttpPost]
public void Post([FromBody] string value)

View File

@ -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;
}
}
}