mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
update readme
This commit is contained in:
parent
b647d02308
commit
9f0e28bcac
@ -14,13 +14,12 @@ namespace dbcontext_01.Controllers
|
|||||||
|
|
||||||
IFreeSql _orm;
|
IFreeSql _orm;
|
||||||
SongContext _songContext;
|
SongContext _songContext;
|
||||||
public ValuesController(SongContext songContext,
|
CurdAfterLog _curdLog;
|
||||||
IFreeSql orm1, IFreeSql orm2,
|
public ValuesController(SongContext songContext, IFreeSql orm1, CurdAfterLog curdLog)
|
||||||
IFreeSql<long> orm3
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
_songContext = songContext;
|
_songContext = songContext;
|
||||||
_orm = orm1;
|
_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 item22 = await _orm.Select<Song>().Where(a => a.Id == id).FirstAsync();
|
||||||
var item33 = await _orm.Select<Song>().Where(a => a.Id > id).ToListAsync();
|
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
|
// GET api/values/5
|
||||||
@ -240,6 +239,14 @@ namespace dbcontext_01.Controllers
|
|||||||
return _orm.Select<Song>().Where(a => a.Id == id).First();
|
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
|
// POST api/values
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public void Post([FromBody] string value)
|
public void Post([FromBody] string value)
|
||||||
|
@ -7,6 +7,7 @@ using System;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace dbcontext_01
|
namespace dbcontext_01
|
||||||
{
|
{
|
||||||
@ -19,49 +20,22 @@ namespace dbcontext_01
|
|||||||
Fsql = new FreeSql.FreeSqlBuilder()
|
Fsql = new FreeSql.FreeSqlBuilder()
|
||||||
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document2.db;Pooling=true;Max Pool Size=10")
|
.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(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")
|
//.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")
|
//.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")
|
//.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)
|
//.UseSyncStructureToUpper(true)
|
||||||
|
|
||||||
.UseAutoSyncStructure(true)
|
.UseAutoSyncStructure(true)
|
||||||
.UseLazyLoading(true)
|
.UseLazyLoading(true)
|
||||||
.UseNoneCommandParameter(true)
|
.UseNoneCommandParameter(true)
|
||||||
|
.UseMonitorCommand(cmd => { }, (cmd, log) => Trace.WriteLine(log))
|
||||||
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText),
|
|
||||||
(cmd, log) => Trace.WriteLine(log)
|
|
||||||
)
|
|
||||||
.Build();
|
.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) =>
|
Fsql.Aop.CurdAfter += (s, e) =>
|
||||||
{
|
{
|
||||||
Console.WriteLine(e.Identifier + ": " + e.EntityType.FullName + " " + e.ElapsedMilliseconds + "ms, " + e.Sql);
|
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 { }
|
enum MySql { }
|
||||||
@ -69,20 +43,14 @@ namespace dbcontext_01
|
|||||||
|
|
||||||
public IConfiguration Configuration { get; }
|
public IConfiguration Configuration { get; }
|
||||||
public static IFreeSql Fsql { get; private set; }
|
public static IFreeSql Fsql { get; private set; }
|
||||||
public static IFreeSql<long> Fsql2 { get; private set; }
|
|
||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddControllersWithViews();
|
services.AddControllersWithViews();
|
||||||
|
|
||||||
services.AddSingleton<IFreeSql>(Fsql);
|
services.AddSingleton<IFreeSql>(Fsql);
|
||||||
services.AddSingleton<IFreeSql<long>>(Fsql2);
|
|
||||||
services.AddFreeDbContext<SongContext>(options => options.UseFreeSql(Fsql));
|
services.AddFreeDbContext<SongContext>(options => options.UseFreeSql(Fsql));
|
||||||
|
services.AddScoped<CurdAfterLog>();
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app)
|
public void Configure(IApplicationBuilder app)
|
||||||
@ -97,4 +65,20 @@ namespace dbcontext_01
|
|||||||
app.UseEndpoints(a => a.MapControllers());
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
40
readme.md
40
readme.md
@ -62,14 +62,14 @@ class Song {
|
|||||||
public string Url { get; set; }
|
public string Url { get; set; }
|
||||||
public DateTime CreateTime { get; set; }
|
public DateTime CreateTime { get; set; }
|
||||||
|
|
||||||
public virtual ICollection<Tag> Tags { get; set; }
|
public ICollection<Tag> Tags { get; set; }
|
||||||
}
|
}
|
||||||
class Song_tag {
|
class Song_tag {
|
||||||
public int Song_id { get; set; }
|
public int Song_id { get; set; }
|
||||||
public virtual Song Song { get; set; }
|
public Song Song { get; set; }
|
||||||
|
|
||||||
public int Tag_id { get; set; }
|
public int Tag_id { get; set; }
|
||||||
public virtual Tag Tag { get; set; }
|
public Tag Tag { get; set; }
|
||||||
}
|
}
|
||||||
class Tag {
|
class Tag {
|
||||||
[Column(IsIdentity = true)]
|
[Column(IsIdentity = true)]
|
||||||
@ -77,10 +77,10 @@ class Tag {
|
|||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
public int? Parent_id { get; set; }
|
public int? Parent_id { get; set; }
|
||||||
public virtual Tag Parent { get; set; }
|
public Tag Parent { get; set; }
|
||||||
|
|
||||||
public virtual ICollection<Song> Songs { get; set; }
|
public ICollection<Song> Songs { get; set; }
|
||||||
public virtual ICollection<Tag> Tags { get; set; }
|
public ICollection<Tag> Tags { get; set; }
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -89,12 +89,11 @@ class Tag {
|
|||||||
//OneToOne、ManyToOne
|
//OneToOne、ManyToOne
|
||||||
fsql.Select<Tag>()
|
fsql.Select<Tag>()
|
||||||
.Where(a => a.Parent.Parent.Name == "粤语")
|
.Where(a => a.Parent.Parent.Name == "粤语")
|
||||||
.IncludeMany(a => a.Tags, then => then.Where(sub => sub.Name == "xxx"))
|
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
//OneToMany
|
//OneToMany
|
||||||
fsql.Select<Tag>()
|
fsql.Select<Tag>()
|
||||||
.Where(a => a.Tags.AsSelect().Any(t => t.Parent.Id == 10))
|
.IncludeMany(a => a.Tags, then => then.Where(sub => sub.Name == "xxx"))
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
//ManyToMany
|
//ManyToMany
|
||||||
@ -127,7 +126,7 @@ fsql.Select<Song>()
|
|||||||
|
|
||||||
fsql.Select<Song>()
|
fsql.Select<Song>()
|
||||||
.OrderBy(a => Guid.NewGuid())
|
.OrderBy(a => Guid.NewGuid())
|
||||||
.Limit(1)
|
.Limit(10)
|
||||||
.ToList();
|
.ToList();
|
||||||
```
|
```
|
||||||
更多前往Wiki:[《表达式函数》](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0)
|
更多前往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<Song>();
|
var repo1 = uow.GetRepository<Song>();
|
||||||
var repo2 = uow.GetRepository<Tag>();
|
var repo2 = uow.GetRepository<Tag>();
|
||||||
|
|
||||||
await repo1.InsertAsync(new Song());
|
repo2.DbContextOptions.EnableAddOrUpdateNavigateList = true;
|
||||||
await repo2.InsertAsync(new Tag());
|
repo2.Insert(new Tag {
|
||||||
uow.Commit();
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## DbContext & DbSet
|
|
||||||
> dotnet add package FreeSql.DbContext
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
using (var ctx = new fsql.CreateDbContext()) {
|
|
||||||
var songs = ctx.Set<Song>();
|
|
||||||
var tags = ctx.Set<Tag>();
|
|
||||||
|
|
||||||
var tag = new Tag {
|
|
||||||
Name = "testaddsublist",
|
Name = "testaddsublist",
|
||||||
Tags = new[] {
|
Tags = new[] {
|
||||||
new Tag { Name = "sub1" },
|
new Tag { Name = "sub1" },
|
||||||
@ -166,10 +152,8 @@ using (var ctx = new fsql.CreateDbContext()) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
//tags.Add(tag);
|
uow.Commit();
|
||||||
ctx.Add(tag);
|
|
||||||
await ctx.SaveChangesAsync();
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user