This commit is contained in:
2881099 2022-08-23 04:15:33 +08:00
commit cd35f476ef
10 changed files with 38 additions and 63 deletions

View File

@ -1,14 +1,10 @@
using FreeSql;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using FreeSql;
using FreeSql.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace aspnetcore_transaction.Controllers
{
@ -25,7 +21,7 @@ namespace aspnetcore_transaction.Controllers
[HttpGet("1")]
//[Transactional]
async public Task<object> Get([FromServices] BaseRepository<Song> repoSong, [FromServices] BaseRepository<Detail> repoDetail, [FromServices] SongRepository repoSong2,
public async Task<object> Get([FromServices] BaseRepository<Song> repoSong, [FromServices] BaseRepository<Detail> repoDetail, [FromServices] SongRepository repoSong2,
[FromServices] SongService serviceSong)
{
//repoSong.Insert(new Song());
@ -39,7 +35,7 @@ namespace aspnetcore_transaction.Controllers
[HttpGet("2")]
//[Transactional]
async public Task<object> GetAsync([FromServices] BaseRepository<Song> repoSong, [FromServices] BaseRepository<Detail> repoDetail, [FromServices] SongRepository repoSong2,
public async Task<object> GetAsync([FromServices] BaseRepository<Song> repoSong, [FromServices] BaseRepository<Detail> repoDetail, [FromServices] SongRepository repoSong2,
[FromServices] SongService serviceSong)
{
await serviceSong.Test2();
@ -77,7 +73,7 @@ namespace aspnetcore_transaction.Controllers
}
[Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
async public Task Test2()
public async Task Test2()
{
await _repoSong.InsertAsync(new Song());
await _repoDetail.InsertAsync(new Detail());
@ -85,7 +81,7 @@ namespace aspnetcore_transaction.Controllers
}
[Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
async public Task<object> Test3()
public async Task<object> Test3()
{
await _repoSong.InsertAsync(new Song());
await _repoDetail.InsertAsync(new Detail());

View File

@ -1,11 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace aspnetcore_transaction
{

View File

@ -1,19 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using aspnetcore_transaction.Controllers;
using FreeSql;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace aspnetcore_transaction
{
@ -22,7 +15,7 @@ namespace aspnetcore_transaction
public Startup(IConfiguration configuration)
{
Configuration = configuration;
Fsql = new FreeSql.FreeSqlBuilder()
Fsql = new FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\test_trans.db")
.UseAutoSyncStructure(true)
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText))

View File

@ -1,13 +1,8 @@
using FreeSql;
using Microsoft.AspNetCore.Mvc.Filters;
using Rougamo.Context;
using System;
using System.Collections.Generic;
using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Rougamo.Context;
namespace FreeSql
{

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FreeSql;
@ -12,9 +11,9 @@ namespace dbcontext_01.Controllers
public class ValuesController : ControllerBase
{
IFreeSql _orm;
SongContext _songContext;
CurdAfterLog _curdLog;
private readonly IFreeSql _orm;
private readonly SongContext _songContext;
private readonly CurdAfterLog _curdLog;
public ValuesController(SongContext songContext, IFreeSql orm1, CurdAfterLog curdLog)
{
_songContext = songContext;
@ -25,10 +24,12 @@ namespace dbcontext_01.Controllers
// GET api/values
[HttpGet]
async public Task<string> Get()
public async Task<string> Get()
{
_orm.SetDbContextOptions(opt =>
{
opt.OnEntityChange = changeReport =>
{
_orm.SetDbContextOptions(opt => {
opt.OnEntityChange = changeReport => {
Console.WriteLine(changeReport);
};
});

View File

@ -13,14 +13,14 @@ namespace FreeSql
{
partial class DbContext
{
async public virtual Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
await FlushCommandAsync(cancellationToken);
return SaveChangesSuccess();
}
static ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], CancellationToken, Task<int>>>> _dicFlushCommandDbSetBatchAsync = new ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], CancellationToken, Task<int>>>>();
async internal Task FlushCommandAsync(CancellationToken cancellationToken)
internal async Task FlushCommandAsync(CancellationToken cancellationToken)
{
if (isFlushCommanding) return;
if (_prevCommands.Any() == false) return;

View File

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using FreeSql.DataAnnotations;
using FreeSql.DataAnnotations;
namespace FreeSql.Extensions.EfCoreFluentApi
{

View File

@ -82,7 +82,7 @@ namespace FreeSql
}
#if net40
#else
async public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
await FlushCommandAsync(cancellationToken);
return SaveChangesSuccess();

View File

@ -103,7 +103,7 @@ namespace FreeSql
_dbset.RemoveRange(entitys);
return _db.SaveChanges();
}
public List<object> DeleteCascadeByDatabase(Expression<Func<TEntity, bool>> predicate)
public virtual List<object> DeleteCascadeByDatabase(Expression<Func<TEntity, bool>> predicate)
{
var list = _dbset.RemoveCascadeByDatabase(predicate);
var affrows = _db.SaveChanges();
@ -151,14 +151,14 @@ namespace FreeSql
return entity;
}
public void SaveMany(TEntity entity, string propertyName)
public virtual void SaveMany(TEntity entity, string propertyName)
{
_dbset.SaveMany(entity, propertyName);
_db.SaveChanges();
}
public void BeginEdit(List<TEntity> data) => _dbset.BeginEdit(data);
public int EndEdit(List<TEntity> data = null)
public virtual void BeginEdit(List<TEntity> data) => _dbset.BeginEdit(data);
public virtual int EndEdit(List<TEntity> data = null)
{
_db.FlushCommand();
if (UnitOfWork?.GetOrBeginTransaction(true) == null && _db.OrmOriginal.Ado.TransactionCurrentThread == null)
@ -206,6 +206,6 @@ namespace FreeSql
public virtual int Delete(TKey id) => Delete(CheckTKeyAndReturnIdEntity(id));
public virtual TEntity Find(TKey id) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOne();
public TEntity Get(TKey id) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOne();
public virtual TEntity Get(TKey id) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOne();
}
}

View File

@ -1,5 +1,4 @@
using FreeSql.Extensions.EntityUtil;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
@ -14,7 +13,7 @@ namespace FreeSql
where TEntity : class
{
async virtual public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
public virtual async Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
{
var delete = _dbset.OrmDeleteInternal(null).Where(predicate);
var sql = delete.ToSql();
@ -32,20 +31,20 @@ namespace FreeSql
_dbset.RemoveRange(entitys);
return _db.SaveChangesAsync(cancellationToken);
}
async public virtual Task<List<object>> DeleteCascadeByDatabaseAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
public virtual async Task<List<object>> DeleteCascadeByDatabaseAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
{
var list = await _dbset.RemoveCascadeByDatabaseAsync(predicate, cancellationToken);
var affrows = await _db.SaveChangesAsync(cancellationToken);
return list;
}
async public virtual Task<TEntity> InsertAsync(TEntity entity, CancellationToken cancellationToken = default)
public virtual async Task<TEntity> InsertAsync(TEntity entity, CancellationToken cancellationToken = default)
{
await _dbset.AddAsync(entity, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
return entity;
}
async public virtual Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys, CancellationToken cancellationToken = default)
public virtual async Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys, CancellationToken cancellationToken = default)
{
await _dbset.AddRangeAsync(entitys, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
@ -63,14 +62,14 @@ namespace FreeSql
return _db.SaveChangesAsync(cancellationToken);
}
async public virtual Task<TEntity> InsertOrUpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
public virtual async Task<TEntity> InsertOrUpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
{
await _dbset.AddOrUpdateAsync(entity, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
return entity;
}
async public Task SaveManyAsync(TEntity entity, string propertyName, CancellationToken cancellationToken = default)
public virtual async Task SaveManyAsync(TEntity entity, string propertyName, CancellationToken cancellationToken = default)
{
await _dbset.SaveManyAsync(entity, propertyName, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
@ -81,7 +80,7 @@ namespace FreeSql
{
public virtual Task<int> DeleteAsync(TKey id, CancellationToken cancellationToken = default) => DeleteAsync(CheckTKeyAndReturnIdEntity(id), cancellationToken);
public virtual Task<TEntity> FindAsync(TKey id, CancellationToken cancellationToken = default) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOneAsync(cancellationToken);
public Task<TEntity> GetAsync(TKey id, CancellationToken cancellationToken = default) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOneAsync(cancellationToken);
public virtual Task<TEntity> GetAsync(TKey id, CancellationToken cancellationToken = default) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOneAsync(cancellationToken);
}
}
#endif