From 9f0e28bcace406ae83e8348244db1c00f2afd85a Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Mon, 9 Mar 2020 19:25:45 +0800 Subject: [PATCH] update readme --- .../Controllers/ValuesController.cs | 17 ++++-- Examples/dbcontext_01/Startup.cs | 56 +++++++------------ readme.md | 40 ++++--------- 3 files changed, 44 insertions(+), 69 deletions(-) diff --git a/Examples/dbcontext_01/Controllers/ValuesController.cs b/Examples/dbcontext_01/Controllers/ValuesController.cs index e8aa5508..7ad083b1 100644 --- a/Examples/dbcontext_01/Controllers/ValuesController.cs +++ b/Examples/dbcontext_01/Controllers/ValuesController.cs @@ -14,13 +14,12 @@ namespace dbcontext_01.Controllers IFreeSql _orm; SongContext _songContext; - public ValuesController(SongContext songContext, - IFreeSql orm1, IFreeSql orm2, - IFreeSql 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().Where(a => a.Id == id).FirstAsync(); var item33 = await _orm.Select().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().Where(a => a.Id == id).First(); } + [HttpGet("get{id}")] + public ActionResult Get2(int id) + { + var item1 = _orm.Select().Where(a => a.Id == id).First(); + var item2 = _orm.Select().Where(a => a.Id == id).First(); + return _curdLog.Sb.ToString(); + } + // POST api/values [HttpPost] public void Post([FromBody] string value) diff --git a/Examples/dbcontext_01/Startup.cs b/Examples/dbcontext_01/Startup.cs index 638fc0f6..b086f8ac 100644 --- a/Examples/dbcontext_01/Startup.cs +++ b/Examples/dbcontext_01/Startup.cs @@ -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(); } enum MySql { } @@ -69,20 +43,14 @@ namespace dbcontext_01 public IConfiguration Configuration { get; } public static IFreeSql Fsql { get; private set; } - public static IFreeSql Fsql2 { get; private set; } public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddSingleton(Fsql); - services.AddSingleton>(Fsql2); services.AddFreeDbContext(options => options.UseFreeSql(Fsql)); - - - var sql1 = Fsql.Update(1).Set(a => a.Id + 10).ToSql(); - var sql2 = Fsql.Update(1).Set(a => a.Title + 10).ToSql(); - var sql3 = Fsql.Update(1).Set(a => a.Create_time.Value.AddHours(1)).ToSql(); + services.AddScoped(); } public void Configure(IApplicationBuilder app) @@ -97,4 +65,20 @@ namespace dbcontext_01 app.UseEndpoints(a => a.MapControllers()); } } + + public class CurdAfterLog : IDisposable + { + public static AsyncLocal Current = new AsyncLocal(); + public StringBuilder Sb { get; } = new StringBuilder(); + + public CurdAfterLog() + { + Current.Value = this; + } + public void Dispose() + { + Sb.Clear(); + Current.Value = null; + } + } } diff --git a/readme.md b/readme.md index 828bcd38..2568f7bb 100644 --- a/readme.md +++ b/readme.md @@ -62,14 +62,14 @@ class Song { public string Url { get; set; } public DateTime CreateTime { get; set; } - public virtual ICollection Tags { get; set; } + public ICollection Tags { get; set; } } class Song_tag { public int Song_id { get; set; } - public virtual Song Song { get; set; } + public Song Song { get; set; } public int Tag_id { get; set; } - public virtual Tag Tag { get; set; } + public Tag Tag { get; set; } } class Tag { [Column(IsIdentity = true)] @@ -77,10 +77,10 @@ class Tag { public string Name { get; set; } public int? Parent_id { get; set; } - public virtual Tag Parent { get; set; } + public Tag Parent { get; set; } - public virtual ICollection Songs { get; set; } - public virtual ICollection Tags { get; set; } + public ICollection Songs { get; set; } + public ICollection Tags { get; set; } } ``` @@ -89,12 +89,11 @@ class Tag { //OneToOne、ManyToOne fsql.Select() .Where(a => a.Parent.Parent.Name == "粤语") - .IncludeMany(a => a.Tags, then => then.Where(sub => sub.Name == "xxx")) .ToList(); //OneToMany fsql.Select() - .Where(a => a.Tags.AsSelect().Any(t => t.Parent.Id == 10)) + .IncludeMany(a => a.Tags, then => then.Where(sub => sub.Name == "xxx")) .ToList(); //ManyToMany @@ -127,7 +126,7 @@ fsql.Select() fsql.Select() .OrderBy(a => Guid.NewGuid()) - .Limit(1) + .Limit(10) .ToList(); ``` 更多前往Wiki:[《表达式函数》](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0) @@ -140,21 +139,8 @@ using (var uow = fsql.CreateUnitOfWork()) { var repo1 = uow.GetRepository(); var repo2 = uow.GetRepository(); - await repo1.InsertAsync(new Song()); - await repo2.InsertAsync(new Tag()); - uow.Commit(); -} -``` - -## DbContext & DbSet -> dotnet add package FreeSql.DbContext - -```csharp -using (var ctx = new fsql.CreateDbContext()) { - var songs = ctx.Set(); - var tags = ctx.Set(); - - var tag = new Tag { + repo2.DbContextOptions.EnableAddOrUpdateNavigateList = true; + repo2.Insert(new Tag { Name = "testaddsublist", Tags = new[] { new Tag { Name = "sub1" }, @@ -166,10 +152,8 @@ using (var ctx = new fsql.CreateDbContext()) { } } } - }; - //tags.Add(tag); - ctx.Add(tag); - await ctx.SaveChangesAsync(); + }); + uow.Commit(); } ```